如何在导航栏添加3个跳转按钮并通过单类实现全局显示
Hey there! Let's break down your two navigation bar questions with clear, actionable steps and code examples tailored for iOS development.
First, you’ll need to create the buttons and assign them to your navigation bar—you can place them on the left, right, or even use a custom title view if you want all three in the middle. Here’s how to do it in Swift:
Step 1: Create the Bar Button Items
Start by defining your three buttons with icons (or text) and linking them to action methods that handle navigation:
// Inside your view controller's viewDidLoad method let profileBtn = UIBarButtonItem( image: UIImage(systemName: "person.circle"), style: .plain, target: self, action: #selector(profileTapped) ) let settingsBtn = UIBarButtonItem( image: UIImage(systemName: "gear"), style: .plain, target: self, action: #selector(settingsTapped) ) let helpBtn = UIBarButtonItem( image: UIImage(systemName: "questionmark.circle"), style: .plain, target: self, action: #selector(helpTapped) )
Step 2: Assign Buttons to the Navigation Bar
You can distribute the buttons across left and right, or stack all three on one side. For example:
// Place one button on the left, two on the right navigationItem.leftBarButtonItem = helpBtn navigationItem.rightBarButtonItems = [profileBtn, settingsBtn] // Or stack all three on the right: // navigationItem.rightBarButtonItems = [profileBtn, settingsBtn, helpBtn]
Step 3: Implement Navigation Actions
Add the selector methods to push the target view controllers:
@objc private func profileTapped() { let profileVC = ProfileViewController() // Replace with your actual VC navigationController?.pushViewController(profileVC, animated: true) } @objc private func settingsTapped() { let settingsVC = SettingsViewController() // Replace with your actual VC navigationController?.pushViewController(settingsVC, animated: true) } @objc private func helpTapped() { let helpVC = HelpViewController() // Replace with your actual VC navigationController?.pushViewController(helpVC, animated: true) }
To avoid repeating code in every view controller, create a custom UINavigationController—this class will manage the navigation bar buttons for all screens pushed into it. Here’s how:
Step 1: Create the Custom Navigation Controller
Make a subclass of UINavigationController and set up the buttons there. We’ll also use the navigation controller delegate to ensure buttons persist when pushing new screens:
import UIKit class CustomNavController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() delegate = self // Set delegate to update buttons on push setupPersistentButtons() } private func setupPersistentButtons() { // Reuse the same button setup as before let profileBtn = UIBarButtonItem(image: UIImage(systemName: "person.circle"), style: .plain, target: self, action: #selector(profileTapped)) let settingsBtn = UIBarButtonItem(image: UIImage(systemName: "gear"), style: .plain, target: self, action: #selector(settingsTapped)) let helpBtn = UIBarButtonItem(image: UIImage(systemName: "questionmark.circle"), style: .plain, target: self, action: #selector(helpTapped)) // Apply buttons to the initial navigation item navigationBar.topItem?.leftBarButtonItem = helpBtn navigationBar.topItem?.rightBarButtonItems = [profileBtn, settingsBtn] } // Navigation actions (same as before) @objc private func profileTapped() { pushViewController(ProfileViewController(), animated: true) } @objc private func settingsTapped() { pushViewController(SettingsViewController(), animated: true) } @objc private func helpTapped() { pushViewController(HelpViewController(), animated: true) } } // Delegate extension to reapply buttons when new VCs are pushed extension CustomNavController: UINavigationControllerDelegate { func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { // Recreate and assign buttons to the incoming view controller's navigation item let profileBtn = UIBarButtonItem(image: UIImage(systemName: "person.circle"), style: .plain, target: self, action: #selector(profileTapped)) let settingsBtn = UIBarButtonItem(image: UIImage(systemName: "gear"), style: .plain, target: self, action: #selector(settingsTapped)) let helpBtn = UIBarButtonItem(image: UIImage(systemName: "questionmark.circle"), style: .plain, target: self, action: #selector(helpTapped)) viewController.navigationItem.leftBarButtonItem = helpBtn viewController.navigationItem.rightBarButtonItems = [profileBtn, settingsBtn] } }
Step 2: Use the Custom Navigation Controller as Your Root
In your SceneDelegate (or AppDelegate for older iOS versions), set your custom navigation controller as the root of your app:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) // Initialize your home view controller let homeVC = HomeViewController() // Wrap it in your custom navigation controller let rootNav = CustomNavController(rootViewController: homeVC) window.rootViewController = rootNav self.window = window window.makeKeyAndVisible() }
Optional: Override Buttons for Specific Screens
If you need to hide or modify the buttons on a specific view controller, just override the navigation items in that VC’s viewWillAppear method:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Hide left button for this screen navigationItem.leftBarButtonItem = nil }
内容的提问来源于stack exchange,提问作者Ayush Agrawal

