2016-04-26 1 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が正しく受信され、他のすべてが正常に動作しますが、UIはこれに応答しないことを知っています。それを別の方法で変更する必要がありますか?

+0

機能が呼び出されていますか? – NSGangster

+0

はい、ただし何も変わりません – sesc360

+0

UIA出現を変更すると、すでに作成されたNavBarのインスタンスではなく、作成された新しいnavBarだけが変更されます。私は現在、UINavigationBarクラス拡張が関係する回答に取り組んでいます。 – NSGangster

答えて

0

あなたのナビゲーションバーは、それがある例ロットの窓のrootViewコントローラである場合、これは動作します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) 
} 

を作成します。そうでない場合は、navBarのインスタンスを取得する別の方法を考え出す必要がありますが、関数の呼び出しは同じです。

+0

ちょうどあなたが色を変えて渡したことを知っていれば、あなたはnavBarでもあります。私は私の答えを更新します。 – NSGangster

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 
} 

ただ、生産にこのようなコードを使用する前に、通知オブザーバ、物事のこれらの種類を削除し、力unwrappingsを削除することを忘れないでください。

+0

@ sesc360あなたのnavigationControllerがあなたのウィンドウのrootViewControllerでない場合、これは私よりも良い答えです。 – NSGangster

+0

UINavigationControllerで拡張機能を作成し、viewDidLoad()をオーバーライドしてaddObserver(self)を追加して、アプリ内のすべてのUINavigationControllerが通知を受け取ることができるのだろうか?そのアプローチ@Kumuluzzにあなたの意見はありますか? – NSGangster

+0

残念ながら、拡張で標準のライフサイクルメソッドをオーバーライドすることはできませんが、 'UINavigationController'を' viewResponsiveNavigationController'のようなサブクラス化して 'viewDidLoad'をオーバーライドしたいと思っています。私は、拡張機能と比較してストーリーボード/コードの可読性が向上し、プッシュに反応しない 'UINavigationController'を持つ可能性もあります。 – Kumuluzz

関連する問題