2017-12-16 16 views
0

1つのView Controllerのナビゲーションバーのみを半透明にし、他の属性を変更しようとしています。Swift 4 1つのView Controllerのナビゲーション属性を設定する

ユーザーがこのVCを離れると、私はAppDelegateに持っているもの、すなわち「正常」に戻ることができます。

viewWillDisappearの各行をリセットする必要がありますか?もしそうなら、私はデフォルトのナビゲーションバーの設定として、バックグラウンドの画像/影の画像に何を使用しますか?

// Make Nav Bar Translucent and Set title font/color 
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    self.navigationController?.navigationBar.shadowImage = UIImage() 
    self.navigationController?.navigationBar.isTranslucent = true 
    self.navigationController?.view.backgroundColor = .clear 
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)] 
+0

を使用することができる利用可能ないくつかのサードパーティ製のcocoapodsがあります。 'viewWillDisappear'。 – matt

答えて

0

はい、あなたはViewWillApearに値を設定し、クリアな透明ナビゲーションバーと通常のカラーナビゲーションバーを持つためviewWillDisappear機能に値をリセットする必要があります。

UINavigationControllerの拡張機能を使用すると、いくつかの列挙型の値に基づいて値を設定/リセットできます。 UIImage上の拡張を作成し、カラーから画像を作成してナビゲーションバーや影の画像に背景画像として適用することができます。

enum NavigationBarMode { 
    case normal 
    case clear 
} 

extension UINavigationController { 

    func themeNavigationBar(mode: NavigationBarMode) { 
     self.navigationBar.isTranslucent = true 
     switch mode { 
     case .normal: 
      let image = UIImage.fromColor(.white) 
      navigationBar.setBackgroundImage(image, for: .default) 
      navigationBar.shadowImage = UIImage.fromColor(.lightGray) 
      navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)] 
     case .clear: 
      let image = UIImage() 
      navigationBar.setBackgroundImage(image, for: .default) 
      navigationBar.shadowImage = image 
      navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)] 
     } 
    } 
} 

extension UIImage { 
    static func fromColor(_ color: UIColor) -> UIImage { 
     let rect = CGRect(x: 0, y: 0, width: 1, height: 1) 
     UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) 
     let context = UIGraphicsGetCurrentContext() 
     context!.setFillColor(color.cgColor) 
     context!.fill(rect) 
     let img = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return img! 
    } 
} 

今はviewWillApearthemeNavigationBar(mode: .transparent)を呼び出し、リセットするためにviewWillDisappearthemeNavigationBar(mode: .normal)を呼び出すことができます。他のviewControllerのナビゲーションバーの設定が共通の場合は、themeNavigationbar(mode:.normal)を呼び出すこともできます。

あなたがあなたの変更を行う前に、viewWillAppear` `でインスタンスプロパティに現在の値を保存する必要があり、それらを復元したい

https://github.com/MoZhouqi/KMNavigationBarTransition https://github.com/DanisFabric/RainbowNavigation

+0

詳細をありがとう。ワウはそれにポッドを探すことさえ考えなかった。私はあなたのソリューションを試しているが、それは型NavigationBarModeはないと言っている? – pmanning

+0

答えを編集し、NavigationBarMode enumを追加して試してみてください – suhit

関連する問題