UIBarbuttonItem 不在工具栏右侧
我的视图控制器中的 viewDidLoad
中有这样的代码。但 UIBarButtonItem
不在工具栏的右侧。它位于左侧。如何给工具栏赋予标题?
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
// create a bordered style button with custom title
UIBarButtonItem *playItem = [[[UIBarButtonItem alloc] initWithTitle:@"Play"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipToSchedules:)] autorelease];
self.navigationItem.rightBarButtonItem = playItem;
NSArray *items = [NSArray arrayWithObjects: playItem, nil];
toolbar.items = items;
// size up the toolbar and set its frame
// please not that it will work only for views without Navigation toolbars.
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect mainViewBounds = self.view.bounds;
[toolbar setFrame:CGRectMake(0, 20,
CGRectGetWidth(mainViewBounds), toolbarHeight)];
[self.view addSubview:toolbar];
I have code in my view controller in viewDidLoad
like this. But the UIBarButtonItem
is not on the right side of the tool bar. It is in left side. How to give title to toolbar also?
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
// create a bordered style button with custom title
UIBarButtonItem *playItem = [[[UIBarButtonItem alloc] initWithTitle:@"Play"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipToSchedules:)] autorelease];
self.navigationItem.rightBarButtonItem = playItem;
NSArray *items = [NSArray arrayWithObjects: playItem, nil];
toolbar.items = items;
// size up the toolbar and set its frame
// please not that it will work only for views without Navigation toolbars.
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect mainViewBounds = self.view.bounds;
[toolbar setFrame:CGRectMake(0, 20,
CGRectGetWidth(mainViewBounds), toolbarHeight)];
[self.view addSubview:toolbar];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
playItem
指定为self.navigationItem
的rightBarButtonItem
没有意义,因为这会将按钮设置为导航栏的按钮顶部,并且您想将其放置在工具栏(底部)中要解决此问题,您必须创建另一个具有灵活宽度的按钮并将其添加为工具栏的第一项:
Assigning the
playItem
as therightBarButtonItem
ofself.navigationItem
does not make sense, since that will set the button as a button of the navigation bar at the top, and you want to place it in the toolbar (at the bottom)To solve this, you'll have to create another button with a flexible width and add that as the first item of the toolbar: