美文网首页
Swift:仿QQ导航栏渐变色背景

Swift:仿QQ导航栏渐变色背景

作者: 姑娘丶你命里缺我 | 来源:发表于2018-08-26 17:42 被阅读0次

    实现的效果图:

    网上所谓的渐变大多数的都是界面滑动时导航栏透明度的改变,特此开一篇文章出来做记录,共同学习

    其实渐变色很简单,利用CAGradientLayer即可。直接上代码:

    letgradientLayer =CAGradientLayer()

    let sizeLength = self.navigationController?.navigationBar.frame.width

    letframeAndStatusBar =CGRect(x:0, y:0, width: sizeLength!, height:64)

    gradientLayer.frame= frameAndStatusBar

    gradientLayer.startPoint=CGPoint(x:0, y:0)

    gradientLayer.endPoint=CGPoint(x:1, y:0)

    gradientLayer.colors = [UIColor.blue.cgColor, UIColor.red.cgColor]

    navigationController?.navigationBar.setBackgroundImage(LayerImage(fromLayer: gradientLayer), for: .default)

    LayerImage函数如下:

    func LayerImage(fromLayer layer:CALayer) ->UIImage{

            UIGraphicsBeginImageContext(layer.frame.size)

            layer.render(in: UIGraphicsGetCurrentContext()!)

            let outputImage = UIGraphicsGetImageFromCurrentImageContext()

            UIGraphicsEndImageContext()

            returnoutputImage!

        }

    相关文章

      网友评论

          本文标题:Swift:仿QQ导航栏渐变色背景

          本文链接:https://www.haomeiwen.com/subject/fshfcxtx.html