如何在自定义视图之间删除UIToolbar中的空格? [英] How do I remove spaces in UIToolbar between custom views?
问题描述
我正在尝试使用自定义图片创建一个带有5个按钮的 UIToolbar
。我这样做的方法是创建 UIButtonTypeCustom
类型的按钮,然后从这些按钮创建 UIBarButtonItems
,然后添加这些使用 setItems:animated:
到工具栏。但是,这会在图像之间添加空格,这会导致第5张图像从工具栏的右侧减半。我如何摆脱这些空间?我已经尝试了我能想到的一切。
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.
非常感谢帮助。
以下是一些示例代码我将如何解决这个问题:
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];
推荐答案
如果您正在寻找的是布局UIBarButtonItems小于默认甚至0垂直间距(像我一样),你自动为你买UIToolBar;以下是我的建议:
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私下布置它的项目。
Apple工程师永远不会期望开发人员覆盖它。
使用固定空格覆盖默认的10Point水平间距是不够的。
由于UIToolbar将其项目至少相隔10个点,因此您将这些UIBarButtonItems的宽度设置为-10,以便不获取间距,如下面的代码所示:
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:
CGFloat toolbarHeight = 44.0;
CGRect toolbarFrame = CGRectMake(0,0, self.view.bounds.size.width, toolbarHeight);
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
UIBarButtonItem* noSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem* noSpace1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
noSpace.width = -10.0;
noSpace1.width = -10.0;
[toolbar setItems:[NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil] autorelease],
noSpace,
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil] autorelease],
noSpace1,
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil] autorelease]
, nil]];
这篇关于如何在自定义视图之间删除UIToolbar中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!