如果我收到推送通知,我会尝试更改导航栏的色调颜色,但它不起作用。
我认为它的工作方式是:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
self.setupUserInterface(color: UIColor(red: 208.0/255.0, green: 2.0/255.0, blue: 27.0/255.0, alpha: 1.0))
}
func setupUserInterface(color color: UIColor) {
// Navigation Bar
UINavigationBar.appearance().barTintColor = color
if let barFont = UIFont(name: "Avenir-Medium", size: 17.0) {
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(), NSFontAttributeName:barFont]
}
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Status Bar
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
我知道PushNotif已正确接收,其他一切正常,但用户界面并未对此做出响应。我是否需要以不同的方式更改它?
答案 0 :(得分:1)
您的代码有效,但仅适用于新实例化的UINavigationController
(您通常不会重新实例化)。这意味着您必须手动更新现有的导航控制器。
有几种方法可以实现这一点,一些更容易,一些更安全,但这是一个使用KVO通知的一个非常简单的例子:
func setupUserInterface(color color: UIColor) {
// Navigation Bar
UINavigationBar.appearance().barTintColor = color
if let barFont = UIFont(name: "Avenir-Medium", size: 17.0) {
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(), NSFontAttributeName:barFont]
}
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Status Bar
UIApplication.sharedApplication().statusBarStyle = .LightContent
// Informs the whole app to refresh if they have a navigation controller
NSNotificationCenter.defaultCenter().postNotificationName("awesomeNotificationKey", object: color)
}
然后在UINavigationController
的子类或导航控制器堆栈中的UIViewController
中注册此通知。此示例位于UIViewController
:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SomeController.refreshNavigationBar(_:)), name:"awesomeNotificationKey", object: nil);
}
func refreshNavigationBar(notification: NSNotification) {
let color = notification.object as! UIColor
navigationController?.navigationBar.barTintColor = color
}
请记住删除强制解包,删除通知观察者和那些事情,然后再在生产中使用这样的代码。
答案 1 :(得分:0)
创建UINavigationBarExtension
extension UINavigationBar {
func didGetNotification(color : UIColor) {
self.barTintColor = color
//Do any other visual changes to current navBar instance here.
}
}
//I would keep most stuff in this function if you want new navBar's to have changes.
func setupUserInterface(color color: UIColor) {
// Navigation Bar
UINavigationBar.appearance().barTintColor = color
if let barFont = UIFont(name: "Avenir-Medium", size: 17.0) {
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor(), NSFontAttributeName:barFont]
}
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Status Bar
UIApplication.sharedApplication().statusBarStyle = .LightContent
// Using self.window assuming you are in AppDelegate.swift
//Grab instance of current navBar and call your function that changes apperance.
(self.window.rootViewController as? UINavigationController).navigationBar.didGetNotification(color)
}
如果您的导航栏是窗口的rootView控制器,这在很多情况下会起作用。如果不是,你将不得不想出另一种方法来获取navBar的实例,但调用该函数是相同的。