UIToolbar UIBarButtonItem 对齐问题

发布于 2024-08-30 22:05:00 字数 376 浏览 4 评论 0原文

我需要创建一个具有两个 UIBarButtonItems 的 UIToolbar。第一个按钮必须居中,第二个按钮必须右对齐。

我理解并使用灵活间距,当我需要平衡 UIToolbar 上的按钮时,它非常有用,但只有两个按钮,我似乎无法将中间按钮完美居中。我什至已经初始化了 view.toolbarItems 数组

NSArray *items = [[NSArray alloc] initWithObjects:fixed, flex, center_button, flex, right_button, nil];

并设置了fixed.width = right_button.width ...但是,center_button 从来没有完全居中。

I need to create a UIToolbar that has two UIBarButtonItems. The 1st button must be centered and the 2nd item must be right aligned.

I understand and use Flexible spacing and it works great when I need to balance buttons across the UIToolbar, but with only two buttons, I can't seem to perfectly center the middle button. I've even initialized the view.toolbarItems array with

NSArray *items = [[NSArray alloc] initWithObjects:fixed, flex, center_button, flex, right_button, nil];

and set fixed.width = right_button.width ... but still, the center_button is never perfectly centered.

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

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

发布评论

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

评论(3

似狗非友 2024-09-06 22:05:00

我最近遇到了同样的问题,并通过创建一种假的 UIBarButtonItem 解决了它。正确对齐的关键是将左右栏按钮的 possibleTitles 属性设置为相同的值,例如:

[right_button setPossibleTitles:[NSSet setWithObject:@"Abc"]];

// Create this fake (plain) button item on the left, to correct the alignment.
UIBarButtonItem *fake = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
[fake setEnabled:NO];
[fake setPossibleTitles:right_button.possibleTitles];

// The array for your toolbar items
NSArray *items = [[NSArray alloc] initWithObjects:fake, flex, center_button, flex, right_button, nil];

迟到的答案...我希望这仍然有帮助。

I had the same problem recently and solved it by creating a sort of fake UIBarButtonItem. The key in getting the alignment right was to set the possibleTitles property of both left and right bar buttons to the same value, for example:

[right_button setPossibleTitles:[NSSet setWithObject:@"Abc"]];

// Create this fake (plain) button item on the left, to correct the alignment.
UIBarButtonItem *fake = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
[fake setEnabled:NO];
[fake setPossibleTitles:right_button.possibleTitles];

// The array for your toolbar items
NSArray *items = [[NSArray alloc] initWithObjects:fake, flex, center_button, flex, right_button, nil];

Late answer... I hope this can still help.

喜你已久 2024-09-06 22:05:00

问题是,UIBarButtonItem 的宽度属性似乎始终为零。 (我认为这是因为系统使用零作为标志,使其成为“适当的”宽度。弹性空间也是如此。)您可能需要做的是在右侧按钮中使用图像(即方式,你知道它的宽度是多少),并将固定空间替换为“左按钮”,该按钮使用与右按钮相同大小的透明图像。

The problem is, a UIBarButtonItem's width property seems to always be zero. (I think it is because zero is used as a flag by the system to make it the "appropriate" width. A flex space is the same way.) What you might have to do is to use an image in your right button (that way, you know what its width will be), and replace your fixed space with a "left button" that uses a transparent image of the same size as the right button.

雄赳赳气昂昂 2024-09-06 22:05:00

听起来要么您的工具栏不是屏幕的整个宽度,要么您的 center_button 未在其框架中居中。您是否尝试过设置 center_button.imageInsets

It sounds like either your toolbar is not the full width of the screen, or your center_button isn't centered in its frame. Have you tried setting center_button.imageInsets?

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