Xcode/Storyboard: 不能将工具栏按钮拖动到顶部的工具栏

我有一个视图控制器,它是一个表的详细视图。当您单击表格的行时,它会将您带到详细信息视图。详细信息视图嵌入在导航控制器中,导航栏左上角有一个按钮,可以将您返回到表中。目前为止还不错。

现在我想在导航栏的右侧添加一个编辑按钮,以便您可以编辑详细信息视图。我的计划是这将增加另一个视图控制器模态,让您编辑的项目的细节。标准程序。

但是,当我尝试将一个条形按钮项从对象列表拖动到导航栏时,它不会执行。相反,当我松开鼠标按钮时,它会在标签栏控制器的底部留下栏按钮。(我的导航方案包括不同的选项卡,每个选项卡都有一个表、详细视图等。)

任何人以前遇到过这种情况,可以建议我做错了什么,或者某种变通方法,将条形按钮项添加到导航屏幕的右侧。我需要加入代码吗?

谢谢你的建议。

43142 次浏览

I believe it's an XCode quirk. I had the same issue. I had to reset Xcode and shift the placement of things on the storyboard to finally get it to go. It's working now.

Did you try "cleaning" your project? I know that helps me sometimes (just go to "Product" > "Clean"). Or, alternatively, delete the navigation bar and try it again.

If you think it's a programming error, feel free to pass on your code. I'd be happy to help in any way I can. :)

Your storyboard's view controller is likely missing a Navigation Item. You can drag one from the Object library, and drop it on your view controller.

You'll then be able to drag and drop bar button items onto the left or right side of the navigation bar.

I have got the same problem using Xcode6 and I noticed that UINavigationItem is added automatically for the first ViewController that you embed into NavigationController but for the subsequent ViewControllers, you will have to add it manually as follows:

  1. In the Object library, find Navigation Item then drag it to your 2nd ViewController in the storyboard.
  2. In the Object library, find Bar Button Item then drag it the navigation item that you have created in the previous step.

Now you will have your Bar Buttons stuck to the top of your scene.

Update The solution still works with XCode 7 but I wanted to add more descriptive photo. Just in case anyone is not really familiar with standard or technical names mentioned above. Notice the red arrows in the right, they refer to step 1, step 2 mentioned above. Also note the hierarchy of views on the left red rectangle. enter image description here

The way that I fixed this issue was, instead of dragging a UINavigationController into the Storyboard and trying to add UIBarButtonItems to that, I dragged in a UITableViewController and then went to

Editor->Embed In->Navigation Controller

Which gave the same result as before except I could add UIBarButtonItems to both sides of the navigation bar with no issues.

Its actually xcode issue. One trick which worked for me is to add the bar button in the VIEW FILE STRUCTURE ON THE LEFT.Instaed adding on the view directly. Hope it helps you.:)

You must reset xCode IDE . Close xCode and write the below codes at terminal...It will work.

rm -rf $HOME/Library/Application Support/Developer/Shared/Xcode
rm -rf $HOME/Library/Preferences/com.apple.dt.Xcode.*
rm -rf $HOME/Library/Saved\ Application\ State/com.apple.dt.Xcode.savedState
rm -rf $HOME/Library/Developer/Xcode

Same issue. I had a Table View Controller inside a Navigation Controller and many ViewControllers following further down the chain. A 'Navigation Item' was being created automatically for the FIRST root view controller and I could add a 'Bar Button Item' to this without a problem. Via the Storyboard, drag and drop. But thats all. On the following Table View Controller and further view controllers, even though they were within the initial Navigation Controller (auto generated back button appeared), storyboard would never let me add a Bar Button Item.

Wael Showair's solution did not work for me. Bar Button Items wouldn't appear.

I solved it programmatically in the end (Swift 2.0):

var testButton = UIBarButtonItem(title: "Test Button", style .Plain, target: self, action: "testButtonMethod")
self.tabBarController?.navigationItem.rightBarButtonItem = testButton

No extra Navigation item or Bar Button Item needed.

I was too facing the same problem.I was able to fix this issue by selecting the root view controller option and then in simulated matrix changed the Top bar option to Opaque Navigation Bar and was able to add the item on top of navigation bar.Hope this answer will help someone.

I also had this problem, close and reopen the project worked for me.

i Solved it. drag in 'Navigation Item' to your detail view. than you can drag in the Bar Button Item.

Nasty trick:

  1. Change your segue to push.
  2. Set the content you need
  3. Set back to show

Then the content will be editable and you have a non deprecated style for segue.

I think it is a bug. I had the same problem.I fixed this problem by disable the size classes, then enable it.

You can disable and enable the size classes in Interface builder doc.

enter image description here

I had the same issue in xcode 8. I had to simply close xcode and reopen the project and then I was able to add the bar button to the Root Controller without any issues.

For XCode 8

There are many answers here. I tried some of them but I couldn't make it. So I found my solution, just select your ViewController, go Attributes Inspector section and change the Top Bar to Opaque Navigation Bar and Boom. You will see navigation bar on your storyboard. You can change the title or add some item. If you don't want to keep Opaque you can change style to Inferred.

enter image description here

I think this is a bug of Xcode. In Xcode 8, change the segue in storyboard before the tab bar view controller to all its options (show detail, present...). Then back to show. Try to drag bar button items and modify tile. It works for me!

Instead of drag the "Bar Button Item" to the "Navigation Controller", drag it to the "Detail View Controller". As you embed the Detail view into the "Navigation View", "Navigation Item" will be added to the "Detail View Controller". If you drag the "Bar Button Item" to the right side of the Navigation Item, the item will be included under the "Right Bar Button Items"

In Xcode 10, I just changed the type of segue going to this new view controller to 'Push (deprecated)', added the navigation item as it was allowed after I made this switch. If you switch back to your originally desired segue type, the navigation item will remain.

like Mark Lyons said , I used the same solution.

  • used push segue first
  • added the bar item
  • return back to show segue
  • done

In Xcode 11, you can drag a button to the navigation bar to create your bar button.