是否可以在 iPhone 上的 UIToolBar 上放置一个按钮?

发布于 2024-10-03 07:58:54 字数 625 浏览 0 评论 0原文

我想在 UIToolBar 的中心位置放置一个按钮。我必须在以下代码中进行哪些更改?

CGRect toolbarFrame = CGRectMake(0, 0, self.view.frame.size.width, 44);
UIToolbar *mytoolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
mytoolbar.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
mytoolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"button 1"
                           style:UIBarButtonItemStylePlain target:self action:nil];
NSMutableArray *tools = [[NSMutableArray alloc] initWithObjects:button,nil];
[mytoolbar setItems:tools];
[self.view addSubview:mytoolbar];

I want to put a button on UIToolBar at the center position. What changes do I have to make in the following code?

CGRect toolbarFrame = CGRectMake(0, 0, self.view.frame.size.width, 44);
UIToolbar *mytoolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
mytoolbar.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
mytoolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"button 1"
                           style:UIBarButtonItemStylePlain target:self action:nil];
NSMutableArray *tools = [[NSMutableArray alloc] initWithObjects:button,nil];
[mytoolbar setItems:tools];
[self.view addSubview:mytoolbar];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

A君 2024-10-10 07:58:55

创建一个 UIBarButtonSystemItemFlexibleSpace 类型的栏按钮。

UIBarButtonItem *spaceButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                             target:nil action:nil];
UIBarButtonItem *button =
    [[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain
                             target:self action:nil];
NSMutableArray *tools =
    [[NSMutableArray alloc] initWithObjects:spaceButton, button, spaceButton,nil];
[mytoolbar setItems:tools];

Create one bar button of UIBarButtonSystemItemFlexibleSpace type.

UIBarButtonItem *spaceButton =
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                             target:nil action:nil];
UIBarButtonItem *button =
    [[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain
                             target:self action:nil];
NSMutableArray *tools =
    [[NSMutableArray alloc] initWithObjects:spaceButton, button, spaceButton,nil];
[mytoolbar setItems:tools];
橘香 2024-10-10 07:58:54

我认为如果您使用 initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace 创建两个 UIBarButtonItems,并将一个放在按钮之前,另一个放在按钮之后 - 您将获得所需的居中位置...

I think if you create two UIBarButtonItems, using initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace, and put one before your button, and the other after -- you'll get the centering that you desire...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文