UIBarbuttonItem 不在工具栏右侧

问题描述 投票:0回答:1

我的视图控制器中有这样的代码。但是

viewDidLoad
不在工具栏的右侧。它位于左侧。如何给工具栏加上标题?
UIBarButtonItem


ios objective-c uitoolbar
1个回答
7
投票
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];

指定为

playItem
rightBarButtonItem
没有意义,因为这会将按钮设置为顶部导航栏的按钮,并且您希望将其放置在工具栏(底部)中)

要解决此问题,您必须创建另一个具有灵活宽度的按钮并将其添加为工具栏的第一项:

self.navigationItem

© www.soinside.com 2019 - 2024. All rights reserved.