UIToolbar UIBarButtonItem 对齐问题
我需要创建一个具有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近遇到了同样的问题,并通过创建一种假的 UIBarButtonItem 解决了它。正确对齐的关键是将左右栏按钮的 possibleTitles 属性设置为相同的值,例如:
迟到的答案...我希望这仍然有帮助。
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:Late answer... I hope this can still help.
问题是,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.
听起来要么您的工具栏不是屏幕的整个宽度,要么您的 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
?