When using this in a view with subviews, the gradient overlays the subviews and hides the content partially. You should replace the addSublayer with the insertSublayer so as to take advantage of the at: property and thus insert the sublayer at position 0.
self.layer.insertSublayer(gradient, at: 0)