100
votes

I have a view controller that is the detail view of a table. When you click on the row of the table it takes you to the detail view. The detail view is embedded in a navigation controller such that there is a button at the upper left of the navigation bar that sends you back to the table. So far so good.

I now want to add an edit button to the right side of the navigation bar so that you can edit the detail view. My plan is this will add another view controller modally that lets you edit the details of the item. Standard stuff.

However, when I try to drag a bar button item from the list of objects to the navigation bar, it won't take. Instead, when I let go off the mouse button, it leaves the bar button on the tab bar controller at the bottom. (My navigation scheme includes different tabs and for each tab a table, detail view etc.)

Anyone run across this before and can suggest what I'm doing wrong or some sort of workaround to add the bar button item to the right side of the navigation screen. Do I have to add it in code?

Thanks for any suggestions.

20
Check the following tutorial brianflove.com/2012/10/01/…casillas
I was able to add the button using self.navigationItem.rightBarButtonItem = self.editButtonItem; and created new editvc in storyboard, however am not sure how to create segue. Can't do it in storyboard because edit button is not visible so I guess it has to be in code. will try prepareforsegue?user1904273
If one of the answers helped you, you should accept it :)user4151918

20 Answers

158
votes

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

70
votes

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.

33
votes

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.

23
votes

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

8
votes

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.

5
votes

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.:)

5
votes

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

4
votes

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

3
votes

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.

3
votes

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!

1
votes

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. :)

1
votes

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
1
votes

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.

1
votes

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.

1
votes

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.

1
votes

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

1
votes

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.

0
votes

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"

0
votes

like Mark Lyons said , I used the same solution.

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

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