gpt4 book ai didi

ios - 导航栏标签栏渐变色

转载 作者:搜寻专家 更新时间:2023-11-01 07:00:06 28 4
gpt4 key购买 nike

这是我为标签栏 Controller 编写的代码:

class TabBarUiViewController: UITabBarController {

let layerGradient = CAGradientLayer()


override func viewDidLoad() {

layerGradient.colors = [UIColor.init(red: 247/255, green: 146/255, blue: 30/255, alpha: 1).cgColor, UIColor.init(red: 236/255, green: 104/255, blue: 66/255, alpha: 1).cgColor]
layerGradient.startPoint = CGPoint(x: 0, y: 0.5)
layerGradient.endPoint = CGPoint(x: 1, y: 0.5)
layerGradient.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
self.tabBar.layer.insertSublayer(layerGradient,at:0)
self.tabBar.unselectedItemTintColor = .black

super.viewDidLoad()

}

}

我想要导航 Controller 栏使用相同的颜色和图案。我应该怎么办?正如我所尝试的那样,它不像标签栏渐变那么简单我正在寻找新的 swift 资源

最佳答案

对于标签栏:使用 addSublayer 而不是 insertSublayer。

类 TabBarUiViewController: UITabBarController {

let layerGradient = CAGradientLayer()

override func viewDidLoad() {
super.viewDidLoad()

layerGradient.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
layerGradient.startPoint = CGPoint(x: 0, y: 0.5)
layerGradient.endPoint = CGPoint(x: 1, y: 0.5)
layerGradient.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
self.tabBar.layer.addSublayer(layerGradient)
}

对于导航栏:

override func viewDidLoad() {

super.viewDidLoad()

self.title = "Gradient Navigation Bar"

if let navFrame = self.navigationController?.navigationBar.frame {

let newframe = CGRect(origin: .zero, size: CGSize(width: navFrame.width, height: (navFrame.height + UIApplication.shared.statusBarFrame.height) ))

let image = gradientWithFrametoImage(frame: newframe, colors: [UIColor.red.cgColor, UIColor.blue.cgColor])!

self.navigationController?.navigationBar.barTintColor = UIColor(patternImage: image)

}
}

func gradientWithFrametoImage(frame: CGRect, colors: [CGColor]) -> UIImage? {
let gradient: CAGradientLayer = CAGradientLayer(layer: self.view.layer)
gradient.frame = frame
gradient.colors = colors
UIGraphicsBeginImageContext(frame.size)
gradient.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}

希望对你有帮助。

关于ios - 导航栏标签栏渐变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345547/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com