如何删除 UIToolbar 中自定义视图之间的空格?
我正在尝试使用自定义图像创建一个包含 5 个按钮的 UIToolbar
。 我这样做的方法是创建 UIButtonTypeCustom
类型的按钮,然后从中创建 UIBarButtonItems
,然后使用 setItems:animated:< 将它们添加到工具栏/代码>。 但是,这会在图像之间增加空格,导致第五张图像超出工具栏右侧的一半。 我如何摆脱这些空间? 我已经尝试了我能想到的一切。
非常感谢您的帮助。
以下是一些关于我如何处理此问题的示例代码:
UIButton *button;
UIBarButtonItem *barButton1,*barButton2;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,button.imageView.image.size.width, button.imageView.image.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"bart_tb.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,button.imageView.image.size.width, button.imageView.image.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
barButton2 = [[UIBarButtonItem alloc] initWithCustomView:button];
NSArray *items = [NSArray arrayWithObjects: barButton1, barButton2, nil];
[self.toolbar setItems:items animated:NO];
I'm trying to create a UIToolbar
with 5 buttons using custom images. The way I'm doing this is by creating buttons of type UIButtonTypeCustom
, then creating UIBarButtonItems
from these, then adding these to the toolbar with setItems:animated:
. However, this adds spaces between the images which cause the 5th image to end up half off the right side of the toolbar. How do I get rid of these spaces? I've tried everything I can think of.
Help is greatly appreciated.
Here's some example code as to how I'm going about this:
UIButton *button;
UIBarButtonItem *barButton1,*barButton2;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,button.imageView.image.size.width, button.imageView.image.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
barButton1 = [[UIBarButtonItem alloc] initWithCustomView:button];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"bart_tb.png"] forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,button.imageView.image.size.width, button.imageView.image.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
barButton2 = [[UIBarButtonItem alloc] initWithCustomView:button];
NSArray *items = [NSArray arrayWithObjects: barButton1, barButton2, nil];
[self.toolbar setItems:items animated:NO];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果你正在寻找的是布局 UIBarButtonItems 小于默认甚至 0 垂直间距(像我一样),那么购买 UIToolBar 就会自动完成; 这是我的建议:
UIToolBar 私密地布局它的项目。
苹果工程师永远不会真正期望开发人员覆盖它。
使用固定间距覆盖默认的 10Point 水平间距是不够的。
由于 UIToolbar 布局其项目至少相距 10 点,因此您将这些 UIBarButtonItems 的宽度设置为 -10,以便没有间距,如以下代码所示:
If you're looking for is to layout UIBarButtonItems with less than the default or even 0 vertical spacing(like me) that's automaticall done for you buy UIToolBar; Here's what I would recommend:
UIToolBar layouts it's items privately.
Apple engineers will never really expect developers to overwrite that.
Using a fixed spaces to overwrite the default 10Point horizontal spacing is not enough.
Since UIToolbar layout its items at least 10 point apart, you set the width of these UIBarButtonItems to -10 in order to get no spacing as the following code shows:
如果您在每个项目之间添加垫片,则间距应该自行解决。 根据您的情况,您可能希望在两个按钮的两侧放置一个垫片,并在其之间放置一个垫片。 您可以这样做:
请注意,您还可以使用 UIBarButtonSystemItemFixedSpace 但您需要显式指定它的“width”属性。 而 UIBarButtonSystemItemFlexibleSpace 似乎可以为您解决这个问题。
If you add spacers in between each item, the spacing should work itself out. In your case you might like to put a spacer either side of the two buttons and one in between. You can do this like so:
Note that you can also use UIBarButtonSystemItemFixedSpace but you would need to specify it's 'width' property explicitly. Whereas UIBarButtonSystemItemFlexibleSpace works this out for you it would seem.
我已经解决了这个问题,你应该在每个按钮之前和之后使用灵活的项目。
只需将 flexSpace 添加到要在工具栏中设置的项目数组中即可。
ps 在摆脱这个之前我几乎自杀了,并且尝试了所有类型的解决方案,包括使用其他栏而不是 UIToolBar。
I have solved this problem, you should use flexible item before and after each button.
Just add the flexibleSpace to your array of items which gonna be set in toolbar.
p.s. I have almost kill myself before get rid of this and have tried all type of solutions including using of other bars instead of UIToolBar.
嗯,这是一个非常老的问题,但我刚刚遇到了同样的问题并设法解决了它。
神奇的密码是将 UIBarButtonItem 样式设置为 UIBarButtonItemStyleBordered。
就是这样...
Well, this is a really old question, but I just ran into the same issue and managed to solve it.
The magic password is setting the UIBarButtonItem style to be UIBarButtonItemStyleBordered.
Thats it...
可以在项目之间添加一个具有负宽度的固定空间项目来补偿间距。
It is possible to add a fixed space item with a negative width between the items to compensate the spacing.
在两个栏按钮之间,您可以选择 UIBarButtonSystemItemFixedSpace。
并调整宽度。它对我有用。
In between two bar button you can take the UIBarButtonSystemItemFixedSpace.
And adjust the width.It has worked for me.
我遇到了同样的问题。 最后,我通过子类化 UIToolbar 并重写布局子视图方法解决了这个问题。
I met the same issue. Finally, I solved this by subclassing UIToolbar and override the layout subviews method.
我能够在 Xcode4 中解决这个问题,方法是将一个灵活的空间作为最左边的项目,然后将另一个空间作为最右边的项目。
I was able to solve this in Xcode4 by putting a flexible space as the left most item, and then another one as the right most item.
这样做......如果您想更改按钮的宽度,请设置 UIBarButtonItem 对象的 width 属性。 宽度的默认值为 0,这使得它足够大以适合其图像/标题。
希望有帮助。
Do it like that.. and if you want to change the width of the buttons, set the width property of the UIBarButtonItem objects. The default value of width is 0, which makes it big enough exactly to fit its image/title.
Hope that helps.
我今天遇到了同样的问题,并用简单的方法解决了它,
所以分享。
您可以采用具有相同编号的新 UIView。 您想要显示的工具栏上的按钮。
然后根据工具栏设置大小即可。
简而言之,工具栏上方的普通按钮看起来像工具栏按钮。
I faced the same problem today and solved it with an easy way ,
So sharing.
You can take a new UIView having the same no. of button on the toolbar you want to show .
and then just set the size according to the toolbar.
In short the normal buttons ABOVE the toolbar will look like toolbar buttons.