How can I create a horizontal gradient background for my iOS nav bar?

前端 未结 4 542
难免孤独
难免孤独 2021-01-31 10:44

I know how to set a navigation bar background color (with barTintColor), but now I am working on an iOS app that calls for a horizontal gradient (not the typical vertical gradie

4条回答
  •  旧时难觅i
    2021-01-31 11:33

    Swift

    Add this function in global class

    func setHorizontalGradientColor(view: UIView) {
        let gradientLayer = CAGradientLayer()
        var updatedFrame = view.bounds
        updatedFrame.size.height += 20
        gradientLayer.frame = updatedFrame
        gradientLayer.colors = [UIColor.black.cgColor, UIColor.blue.cgColor]
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
        view.layer.insertSublayer(gradientLayer, at: 0)
    }
    

    Use in anywhere:

    setHorizontalGradientColor(view: self.bgView)
    

提交回复
热议问题