【问题标题】:How to change Nav Bar Controller Color for every VC based on user choice?如何根据用户选择更改每个 VC 的导航栏控制器颜色?
【发布时间】:2016-12-21 15:59:49
【问题描述】:

在我的应用委托中,我调用以下代码将每个 VC 的导航栏颜色设置为蓝色。但是,如果用户没有登录并且只是在试用该应用程序,我希望导航栏为红色。

UINavigationBar.appearance().barTintColor = UIColor(red: 108.0/255.0, green: 158.0/255.0, blue: 236.0/255.0, alpha: 1.0) // Blue

//    UINavigationBar.appearance().barTintColor = UIColor(red: 239.0/255.0, green: 119.0/255.0, blue: 97.0/255.0, alpha: 1.0) // Red

UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

我将如何根据用户对每个 VC 的选择来更改颜色?

【问题讨论】:

  • 检查您的应用代理是否已登录
  • 在每个 VC 中调用这个 navigationController?.navigationBar.barTintColor = UIColor.blue
  • 如果有任何问题请告诉我
  • 我认为您的意思是“导航栏控制器色调颜色”

标签: ios swift uinavigationbar


【解决方案1】:

像这样在viewWillAppear 方法中更改每个 VC 中的颜色

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    navigationController?.navigationBar.barTintColor = UIColor(red: 0/255.0, green: 210/255.0, blue: 255/255.0, alpha: 1.0)
 }

如果您正在等待某个事件发生,那么您可以在完成后更改。像这样

navigationController?.navigationBar.barTintColor = UIColor(red: 0/255.0, green: 210/255.0, blue: 255/255.0, alpha: 1.0)

【讨论】:

  • 谢谢,我使用 NSUserDefaults 来获得我需要的东西
  • 嗯。欢迎:)
【解决方案2】:

UIViewController 的以下扩展添加到您的项目中,注意这是“在所有视图控制器上运行相同代码”的最简单、最直接的方法,但它使用的是方法调配。虽然我在下面编写的代码是完全安全的,但如果您不知道自己在做什么,它可能会导致不希望的/意外的行为。 谨慎行事。

extension UIViewController {
    public override class func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }

        // make sure this isn't a subclass
        if self !== UIViewController.self {
            return
        }

        dispatch_once(&Static.token) {
            let originalSelector = Selector("viewWillAppear:")
            let swizzledSelector = Selector("extended_viewWillAppear:")

            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

            let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

            if didAddMethod {
                class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
        }
    }

    func extended_viewWillAppear(animated: Bool) {
        self.extended_viewWillAppear(animated)

        // Call your code here that you want to run for all view controllers, table view controllers, tab view controllers etc...
        navigationController?.navigationBar.barTintColor = UIColor(red: 0/255.0, green: 210/255.0, blue: 255/255.0, alpha: 1.0)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2016-06-06
    • 2017-06-20
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多