如何在ios 8中添加自定义页脚(带渐变)到UIView

时间:2014-12-20 16:31:47

标签: ios swift uiview

我可以像这样在UIView中添加自定义状态栏(带渐变):

override func viewDidLoad() {
    super.viewDidLoad()
    var statusBar : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 20))
    let statusBarGradient : CAGradientLayer = CAGradientLayer()
    statusBarGradient.frame = statusBar.bounds
    let cor1 = UIColor(red: 0.416, green: 0.604, blue: 0.796, alpha: 1.0)
    let cor2 = UIColor.whiteColor()
    let arrayColors = [cor1.CGColor, cor2.CGColor]
    statusBarGradient.colors = arrayColors
    view.layer.insertSublayer(statusBarGradient, atIndex:0)
}

我还想在页脚中添加相同的渐变,但我没有太多运气。

1 个答案:

答案 0 :(得分:2)

如果你将statusBar添加为视图的子视图,你的代码是有效的,但我会动态调整宽度以适应超视图的宽度,所以只需为页脚做同样的事情,但动态设置高度无论您的视图高度减去所需的页脚栏高度。

let statusBar = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 20))
let footer = UIView(frame: CGRect(x: 0, y: view.frame.height - 20.0, width: view.frame.width, height: 20))
let statusBarGradient = CAGradientLayer()
let footerGradient = CAGradientLayer()

statusBarGradient.frame = statusBar.bounds
let cor1 = UIColor(red: 0.416, green: 0.604, blue: 0.796, alpha: 1.0)
let cor2 = UIColor.blackColor()
let arrayColors = [cor1.CGColor, cor2.CGColor]

footerGradient.frame = footer.bounds

let arrayColorsFooter = [cor2.CGColor, cor1.CGColor]
statusBarGradient.colors = arrayColors
footerGradient.colors = arrayColorsFooter

statusBar.layer.insertSublayer(statusBarGradient, atIndex:0)
footer.layer.insertSublayer(footerGradient, atIndex:0)
view.addSubview(statusBar)
view.addSubview(footer)
相关问题