[英]navigationController rules doesn't work on ViewController
My problem is with the navigationController
the rules just don't apply. 我的问题是navigationController
规则不适用。
override func viewDidLoad() {
super.viewDidLoad()
navigationBarColor()
}
func navigationBarColor() {
navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
}
Here's the result - The color is not there 结果如下-颜色不存在
What should I do? 我该怎么办?
Your code setting the style properties looks correct. 您设置样式属性的代码看起来正确。 It's likely that navigationController
is nil. navigationController
可能为nil。
A better approach is to use a custom navigation controller subclass and connect it to the navigation controller in Interface builder. 更好的方法是使用自定义导航控制器子类,并将其连接到“界面”构建器中的导航控制器。
open class NavigationController: UINavigationController {
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.barTintColor = UIColor.blue
}
}
first you set bartintColor and after that you set the "clear image" in navigation bar :) of course it is be transparent. 首先,您设置bartintColor,然后在导航栏中设置“清晰图像” :)当然,它是透明的。
you need image with some color or create it in code: (for example) 您需要某种颜色的图像或用代码创建它:(例如)
extension UIImage {
class func imageWithColor(_ color: UIColor) -> UIImage {
let rect = CGRect(origin: CGPoint.zero, size: CGSize(width: 1.0, height: 1.0))
UIGraphicsBeginImageContext(rect.size);
guard let context = UIGraphicsGetCurrentContext() else {
UIGraphicsEndImageContext()
return UIImage()
}
context.setFillColor(color.cgColor);
context.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
}
and set image in navigation bar like this: 并在导航栏中设置图像,如下所示:
call this navigationBar changes when controller will appearing 当控制器出现时调用此navigationBar更改
func updatenavigationBar() {
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage.imageWithColor(color), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
I'm not quite sure what you're asking based on the question but if you are trying to change the color of the navigation bar I would suggest changing : 我不确定您要问的问题是什么,但是如果您要更改导航栏的颜色,我建议您更改:
navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
To: 至:
navigationController?.navigationBar.backgroundColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
This will add the color as the background color instead of just the tint color. 这会将颜色添加为背景颜色,而不只是色调颜色。
Use this code 使用此代码
import Foundation
import Swift
extension UINavigationController
{
func setMainTopNavigationBarAttribute() -> Void
{
self.navigationBar.shadowImage = UIImage()
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.isTranslucent = false
self.navigationBar.tintColor = UIColor.white
self.navigationBar.backgroundColor = UIColor.white
}
}
You can use this way 你可以用这种方式
let navigationController = UIStoryboard.user.instantiateViewController(withIdentifier: "MMNavigationController") as! UINavigationController
navigationController.setMainTopNavigationBarAttribute()
You just change this two line in your function
func navigationBarColor() {
navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.04, blue:0.51, alpha:1.0)
//change this two line in your function
navigationController?.navigationBar.shadowImage = UIImage.init(named: "")
navigationController?.navigationBar.setBackgroundImage(UIImage.init(named: ""), for: .default)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.