UIBarbuttonItem 不在工具栏右侧

发布于 2024-08-07 22:28:42 字数 984 浏览 8 评论 0原文

我的视图控制器中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-08-14 22:28:42

playItem 指定为 self.navigationItemrightBarButtonItem 没有意义,因为这会将按钮设置为导航栏的按钮顶部,并且您想将其放置在工具栏(底部)中

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

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:flexible, playItem, nil];
[flexible release];

Assigning the playItem as the rightBarButtonItem of self.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:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:flexible, playItem, nil];
[flexible release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文