Ios – Can you create a UIBarButtonItem with a Custom View in Interface Builder

interface-builderios

I know how to use initWithCustomView to create and add a UIBarButtonItem with a custom view in code. But I prefer to do things like this in Interface Builder when I can. I can't seem to find any way to create a custom UIBarButtonItem and set the custom view.

Best Answer

OK, I eventually figured out how to do it.

If you drag a view onto a NavigationItem that will create a UIBarButtonItem that is initialized with that custom view and it links it to the right bar button. Alternatively you can drag the view onto a bar button item that is inside a Navigation Item. It will not work for a bar button item that is not inside a Navigation Item.

To create a left bar item with custom view, it is a bit trickier. A bar button item inside a navigation item is hardcoded to be the right side item. What I did was add another navigation item temporarily to the top level of the xib file. Then drag the view onto the navigation item which creates the bar button item inside this navigation item. Move the bar button item out of the navigation item (to the top level of the xib). Then I can delete the navigation item I added so then I have a free standing bar button item initialized with a custom view. You then have to add the link from the leftBarButtonItem outlet of the real navigation item to this bar button item yourself.