![](/img/trans.png)
[英]How to change the background color of a custom Collection View cell in swift iOS?
[英]How to change background view color partialy in ios swift
迅速掌握新功能,我希望背景视图的80%高度为红色,其余部分为蓝色。 目前,我正在添加两个视图。 我认为有直接的方法可以做到这一点。 任何指导或帮助将不胜感激。
添加两个视图是获得所需内容的好方法(也是最好的方法,因为您可以通过约束设置%。)
另一种方法是通过代码将子层添加到视图中:
let gradient: CAGradientLayer = CAGradientLayer()
let topColor = UIColor(red:223.0/255.0, green:142.0/255.0, blue:219.0/255.0, alpha:255.0/255.0).cgColor
let bottomColor = UIColor(red:0, green:201.0/255.0, blue:243.0/255.0, alpha:255.0*0.34).cgColor
gradient.colors = [topColor, UIColor.clear, bottomColor]
gradient.locations = [0.0 , 0.5, 1.0]
gradient.frame = self.view.bounds
self.view.layer.insertSublayer(gradient, at: 1)
如果您想做一些像梯度这样的时髦的东西,我只会使用layer方法。
感谢帮助。 我想我找到了解决方案。添加子层可以解决问题。
self.view.backgroundColor = UIColor.red
let bottomBorder = CALayer()
let t = CGRect(x: 0.0, y: (self.view.frame.size.height/100.0)*80, width: self.view.frame.size.width, height: (self.view.frame.size.height/100.0)*20)
bottomBorder.frame = t
bottomBorder.backgroundColor = UIColor.blue.cgColor
self.view.layer.addSublayer(bottomBorder)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.