2016-12-21 1 views
0

私のアプリケーションデリゲートでは、すべてのVCのナビゲーションバーの色を青に設定する次のコードを呼び出します。しかし、ユーザーがログインしていない場合、スピンのためのアプリを取っているだけで、ナビゲーションバーが赤色になりたい。ユーザーの選択に基づいてすべてのVCのNav Bar Controller Colorを変更するにはどうすればよいですか?

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のユーザーの選択に基づいて色を変更するにはどうすればよいですか?

+0

アプリのデリゲートでのチェックユーザーがログインしていることにあるかどうか、すべてのVCで –

+0

コールこの 'navigationController?.navigationBar.barTintColor = UIColor.blue' –

+0

すべての問題 –

答えて

1
あなたには、いくつかのイベントを待っている場合は、この

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) 
} 

ようviewWillAppear方法では、すべてのVCであなたの色を変更

、あなたがそれを完了した後に変更されることがあり起こります。この

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

+0

ありがとう、NSUserDefaultsを使って私が必要なものを手に入れました – RubberDucky4444

+0

Hmmmm。ようこそ :) –

0

のように、この点に注意してください、あなたのプロジェクトに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) 
    } 
} 
関連する問題