UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor blackColor].CGColor];
[view.layer insertSublayer:gradient atIndex:0];
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
let gradient = CAGradientLayer()
gradient.frame = view.bounds
gradient.colors = [UIColor.white.cgColor, UIColor.black.cgColor]
view.layer.insertSublayer(gradient, at: 0)
Info:use startPoint and endPoint to 100.
如果在这个UIView
(如UILabel
)上添加了任何其他视图,那么您可能需要考虑将那些UIView
的背景 colored颜色 设置为[UIColor clearColor]
,从而呈现渐变视图而不是子视图的背景 colored颜色 .使用clearColor
对性能有轻微影响.