iPhone 工具栏按钮间距

发布于 2024-08-31 15:58:08 字数 43 浏览 1 评论 0原文

有没有办法让工具栏中的 UIBarButtonItems 均匀地间隔开?

Is there a way to have the UIBarButtonItems in a toolbar space themselves out evenly?

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

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

发布评论

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

评论(3

零時差 2024-09-07 15:58:08

在 UIBarButtonItems 之间放置一个灵活的空格键按钮项。这在 IB 中很容易做到,请查看控件的底部。

如果您想以编程方式执行此操作,此代码应该有所帮助:

UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:@"Button1" style:UIBarButtonItemStyleBordered target:self action:@selector(button1Action)];
UIBarButtonItem* button2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStyleBordered target:self action:@selector(button2Action)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[myToolbar setItems:[NSArray arrayWithObjects:button1, flexibleSpace, button2, nil]];

Drop a Flexible Space bar button item in between your UIBarButtonItems. This is pretty easy to do in IB, look down the bottom of the controls.

If you want to do this programtically, this code should help:

UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:@"Button1" style:UIBarButtonItemStyleBordered target:self action:@selector(button1Action)];
UIBarButtonItem* button2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStyleBordered target:self action:@selector(button2Action)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[myToolbar setItems:[NSArray arrayWithObjects:button1, flexibleSpace, button2, nil]];
怎言笑 2024-09-07 15:58:08

忽略 UIBarButtonItem 建议的宽度;这不是 Apple 推荐的正确方法,如果您想添加更多图标,这将不起作用。

正确的方法是在每个按钮之间添加一个“灵活空间”(技术上是另一个按钮!)。您可以在 Interface Builder 中看到它,或者如果需要,也可以直接在代码中添加它。

Ignore the width on UIBarButtonItem suggestion; this is not the correct approach as recommended by Apple and will not work if you want to add further icons.

The correct approach is to add a "Flexible space" (technically another button!) in between each button. You see it in Interface Builder, or it can be added directly in code if needed.

铁憨憨 2024-09-07 15:58:08

是的。使用 UIBarButtonSystemItemFlexibleSpace 通过 -initWithBarButtonSystemItem: 方法创建一个 UIBarButtonItem,并将其插入到每个实际的工具栏项之间。例如:

UIBarButtonItem *flexSpace = [[UIBarButton alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace];
myToolbar.items = [NSArray arrayWithObjects:buttonOne,flexSpace,buttonTwo,flexSpace,buttonThree,nil];
[flexSpace release];

Yup. Create a UIBarButtonItem with the -initWithBarButtonSystemItem: method using UIBarButtonSystemItemFlexibleSpace, and insert that between each of your actual toolbar items. E.g.:

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