I have a UITableViewCell
that has an UIView
(gradientView
) where I want to apply the gradient. This cell has AutoLayout, and so I want to update the gradient frame when the gradientView
frame changes.
I have tried the following, but none of them worked:
-(void)layoutSubviews{
[super layoutSubviews];
self.gradient.frame = self.gradientView.bounds;
}
-(void)updateConstraints{
[super updateConstraints];
self.gradient.frame = self.gradientView.bounds;
}
-(void)updateConstraintsIfNeeded{
[super updateConstraintsIfNeeded];
self.gradient.frame = self.gradientView.bounds;
}
also I can't use this inside the cell:
- (void)viewDidLayoutSubviews{}
I'd suggest create a new
UIView
subclass whose backing layer (layerClass
) is aCAGradientLayer
. Apply the colors as needed. Whenever the view'sbounds
change, iOS will internally resize thelayer
too.If you want to make it even simpler, just grab
OBGradientView
from here and use it in your project. https://github.com/ole/OBGradientView