开发者

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

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开发者_StackOverflow view.


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.


As he said in the comments, user486646's solution no longer works in Xcode 6. After messing around a bit this is what I figured out.

1) Drag a toolbar into your View Controller Scene. Not into the view, but into the scene, eg. under Exit.

2) Drag your custom view into (under) the toolbar. This will create the the UIBarButtonItem with embedded custom view in the toolbar.

3) Now drag the the UIBarButtonItem with embedded custom view to wherever you really want it (eg. navigation item or just as one of the View Controller's toolbar items).

4) Delete the toolbar created in step 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜