简体   繁体   中英

Best way to share nav bar across multiple views?

I have three view controllers that I want to have the same navigation bar on but I can't figure out a way to "share" the navbar between those three views. Is there a more efficient way to link up the nav controllers with the view controllers so that I don't have three different navigation controllers even though the navigation controllers are exactly the same?

In addition, I have a tab bar controller if that makes a difference.

Picture to clarify:

在此输入图像描述

As Ch0k0I8 mentioned, UIAppearance is a good way to set a common UI theme throughout the app.

If you are looking for the same functionality between navigation controllers, then you should subclass UINavigationController and implement the common functionality there. Then you can use your custom navigation controller in the storyboard or in code in the 3 places you wish. To put your custom subclass in the storyboard, drag a UINavigationController onto the storyboard like you normally would, then change the class to your custom navigation controller class in the utilities tab. Here is what a custom UINavigationController subclass might look like:

class CustomNavController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // do whatever custom setup stuff you want here
    }

    // override other methods for different customizations
}

You're using a storyboard, and from what you're describing, you don't need three nav controllers. Instead, connect the three UIViewControllers by ctrl-dragging from one to the next to create a push segue. Don't use the UIAppearance proxy; just set customize the bar's appearance in your navigation controller's rootViewController 's viewDidLoad method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM