2016-04-26 38 views
1

如果我收到推送通知,但嘗試更改我的導航欄的色調但不起作用。當收到PushNotif時,在AppDelegate中更改NavBar的着色顏色

我想它的工作方式是:

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

你的函數被調用了嗎? – NSGangster

+0

是的,但沒有任何變化 – sesc360

+0

更改UIAppearance只會改變製作的新navBar,而不是NavBar已經制作的實例。我現在正在處理一個涉及UINavigationBar類擴展的答案。 – NSGangster

回答

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的實例,但調用該函數是一樣的。

+0

剛剛意識到你通過顏色來改變你的navBar。我會更新我的答案。 – NSGangster

1

您的代碼有效,但只適用於新實例化的UINavigationController(通常不會重新實例化)。這意味着您必須手動更新現有的導航控制器。

有實現這個幾個方面,有的容易,有的更安全,但這裏是用志願通知一個非常簡單的例子:

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 
} 

只記得刪除力unwrappings,刪除通知觀察者和這種事情在生產中使用這樣的代碼之前。

+0

@ sesc360這比我的回答更好,特別是如果你的navigationController不是你窗口的rootViewController。 – NSGangster

+0

我不知道你是否可以在UINavigationController中創建一個擴展,重寫viewDidLoad()和addObserver(self),這樣應用程序中的每個UINavigationController都會收到通知?你對這種方法會有什麼看法@Kumuluzz? – NSGangster

+0

不幸的是無法重寫擴展中的標準生命週期方法,但無論如何我寧願將'UINavigationController'繼承到類似'PushResponsiveNavigationController'的東西,我會在其中重寫'viewDidLoad'。我比較喜歡這種擴展,原因有兩個:與擴展相比,它具有更好的故事板/代碼的可讀性,並且還允許具有不響應推送的「UINavigationController」。 – Kumuluzz