【问题标题】:How to add custom footer (with gradient) in ios 8 to UIView如何在 ios 8 中将自定义页脚(带渐变)添加到 UIView
【发布时间】:2014-12-20 16:31:47
【问题描述】:

我可以像这样向 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)
}

我还想在页脚中添加相同的渐变,但运气不佳。

【问题讨论】:

    标签: ios swift uiview


    【解决方案1】:

    如果您将 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      相关资源
      最近更新 更多