Xcode中无法为Navigation Controller添加Bar Button Item求助
Hey Andrew, sorry you're stuck with this frustrating bar button item problem—let’s walk through the most likely causes and fixes to get things back on track:
Common Causes & Solutions
1. Your Add/Edit View Controllers Lack a Proper Navigation Bar
If your add/edit VCs aren’t embedded in a navigation controller (and you haven’t manually added a navigation bar to them), Xcode will default to placing bar button items on any existing bar in the VC—like an accidentally enabled bottom toolbar, or even a tab bar if one exists in your project.
- Fix:
- Open your storyboard, select the problematic add/edit VC.
- Go to the Editor menu → Embed In → Navigation Controller. This automatically adds a navigation bar to the VC, where your bar button items belong.
- Alternatively, if you don’t want a full navigation controller embed, drag a
Navigation Barfrom the Object Library onto the top of your add/edit VC, then reposition your bar button items onto this bar.
2. You Accidentally Enabled a Bottom Toolbar
It’s easy to toggle the toolbar setting for a VC without noticing, which makes Xcode route bar button items to the bottom instead of a navigation bar.
- Fix:
- Select your add/edit VC in the storyboard.
- Open the Attributes Inspector (right-hand panel, slider icon).
- Scroll to the View Controller section and uncheck the Toolbar option if it’s enabled.
3. Incorrect Segue Configuration
If you’re presenting your add/edit VC using a present segue instead of a push segue, the VC won’t inherit the main navigation controller’s navigation bar by default.
- Fix:
- If you want to keep using a present segue: Embed your add/edit VC in its own navigation controller first, then present that navigation controller instead of the VC directly.
- If your app flow allows it: Switch the segue type to
push—since your main VC is already in a navigation controller, this will add the add/edit VC to the navigation stack with a proper navigation bar.
4. Misplaced Bar Button Items in Storyboard
It’s easy to misdrag bar button items to the bottom of the screen instead of the navigation bar, especially if Xcode’s layout hints aren’t showing up clearly.
- Fix:
- Check the Document Outline (left-hand panel) to see where your bar button item is nested. It should be a child of the
Navigation Bar, not aToolbaror the main view. - Drag the bar button item up to the navigation bar—you’ll see blue alignment lines when it’s in the correct left/right position on the bar.
- Check the Document Outline (left-hand panel) to see where your bar button item is nested. It should be a child of the
5. Storyboard Cache Glitch
Even after restarting Xcode, cached storyboard data can cause odd layout issues.
- Fix:
- Clean your project: Go to Product → Clean Build Folder (hold the Option key to see this option).
- Delete derived data: Go to Xcode → Settings → Locations → Click the arrow next to Derived Data to open the folder, then delete the folder for your project.
If none of these steps fix the issue, diving into your storyboard setup in the code repo would help narrow things down further—but these cover the most common culprits for this exact problem.
内容的提问来源于stack exchange,提问作者Andrew Tuzson

