How can I add a UIToolbar to a UIViewController?

2019-07-06 17:04发布

I have a UIViewController. I want to add a UIToolbar to the bottom, but IB is not letting me. How can I achieve this?

Note: The UIViewController is part of a UINavigationController. I'm not sure if this affects the visibility of the toolbar.

2条回答
Summer. ? 凉城
2楼-- · 2019-07-06 17:45

You can't add toolbar in IB, in IB all you can do is - set a dummy toolbar / navigation bar / tabBar etc. so that you can simulate them to arrange your view's subviews appropriately.

To set the toolbar for that viewController, you have to fill the UIViewController's:

setToolbarItems:animated:

or

@property(nonatomic, retain) NSArray *toolbarItems

so this toolbar and its items will be displayed when your viewController is presented.

查看更多
Ridiculous、
3楼-- · 2019-07-06 17:48

Starting from iOS 3.0 you can set toolbar items you want using following UIViewController's method:

- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated

of simply set or override toolbarItems property.

And toolbar will appear automatically if you put your view controller inside navigation controller.

查看更多
登录 后发表回答