gpt4 book ai didi

ios - iOS 11 中带有大标题导航栏的自定义背景图片

转载 作者:IT王子 更新时间:2023-10-29 05:12:25 50 4
gpt4 key购买 nike

iOS 11大标题NavigationBar如何设置自定义背景图?我正在使用一个自定义子类,我已将其分配给 Storyboard 中的 navigationControllers。

这就是我创建自定义导航栏的方式:

class CustomNavigationController: UINavigationController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
self.navigationBar.tintColor = UIColor(red:1, green:1, blue:1, alpha:0.6)
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
if #available(iOS 11.0, *) {
self.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .automatic
self.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
self.navigationBar.barTintColor = UIColor.green
}
self.navigationBar.isTranslucent = false
self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "navigationBarBackground"), for: .default)
self.navigationBar.shadowImage = #imageLiteral(resourceName: "navigationBarShadow")
}
}

奇怪的是,setBackgroundImage(image, for: .default) 不适用于大标题。它以前适用于 iOS 10,如果我旋转 iPhone(并激活小 NavBar),背景又回来了?

编辑:backgroundImage 仍然呈现但不知何故被隐藏了。只有当您开始滚动并且出现“正常”导航栏时,背景图像才可见。在这种情况下,barTintColor 也被完全忽略。 screenshot GIF

最佳答案

我遇到了同样的问题,已解决

删除 setBackgroundImage 并使用带有图案图像的 barTint 颜色

let bgimage = imageWithGradient(startColor: UIColor.red, endColor: UIColor.yellow, size: CGSize(width: UIScreen.main.bounds.size.width, height: 1))
self.navigationBar.barTintColor = UIColor(patternImage: bgimage!)

获取带有渐变颜色的图像

func imageWithGradient(startColor:UIColor, endColor:UIColor, size:CGSize, horizontally:Bool = true) -> UIImage? {

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
if horizontally {
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
} else {
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
}

UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}

关于ios - iOS 11 中带有大标题导航栏的自定义背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46196848/

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