これは以前に尋ねられた質問とは少し異なる質問です。 だからいいね。。アプリケーション全体に対してUINavigationBarをカスタマイズする方法は?
- アプリ全体にさまざまな色のナビゲーションバーがあります。
- アプリ全体には、ナビゲーションバーにはほとんどオプションがありません。彼らはすべて同じです。
とは何ですか?
私はUINavigationController
のためにこの拡張を作成しました。私は表示コントローラのようにナビゲーションバーの背景色を変更することができます。
extension UINavigationController {
func updateNavigationBar(withViewControllerID identifier: String?) {
setupNavigationBarButtons()
if identifier == kFirstVCIdentifier {
self.navigationBar.tintColor = .white
self.navigationBar.barTintColor = .red
} else if identifier == kSecondVCIdentifier {
self.navigationBar.tintColor = .white
self.navigationBar.barTintColor = .green
} else if identifier == kThirdVCIdentifier {
self.navigationBar.tintColor = .white
self.navigationBar.barTintColor = .blue
}
self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
self.navigationBar.isTranslucent = false
self.navigationBar.clipsToBounds = false
self.navigationBar.shadowImage = UIImage.init()
self.view.backgroundColor = .clear
}
internal func setupNavigationBarButtons() {
let barButtonItemSettings = UIBarButtonItem.init(title: "Settings", style: .plain, target: self, action: #selector(actionSettings))
let barButtonItemSpace = UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let barButtonItemLogOut = UIBarButtonItem.init(title: "LogOut", style: .plain, target: self, action: #selector(actionLogOut))
let buttons = [barButtonItemSettings, barButtonItemSpace, barButtonItemLogOut]
self.navigationItem.rightBarButtonItems = buttons
}
@objc internal func actionSettings() {
print("Settings")
}
@objc internal func actionLogOut() {
print("LogOut")
}
}
そして、私はその拡張内UIBarButton
を追加しようとしていますが、彼らは表示されません。だから私はそれを修正する必要があります。それが見えるようになったら、私はUIBarButtonのタッチイベントをアプリケーション全体で処理できるように、どのようにそのアクションを呼び出すのですか? 正しく?パッチはありません。
私はこのようにそれを使用しています:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.updateNavigationBar(withViewControllerID: self.restorationIdentifier?)
}
あなたは、ナビゲーションバーを非表示にし、カスタムビュー –
を作ることができ、あなたがこれを必要とする理由、それは –