[翻译]  How do I remove spaces in UIToolbar between custom views?

[CHINESE]  如何在自定义视图之间删除UIToolbar中的空格?


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.

我正在尝试使用自定义图像创建一个带有5个按钮的UIToolbar。我这样做的方法是创建UIButtonTypeCustom类型的按钮,然后从这些按钮创建UIBarButtonItems,然后使用setItems:animated:将它们添加到工具栏中。但是,这会在图像之间添加空格,这会导致第5张图像从工具栏的右侧减半。我如何摆脱这些空间?我已经尝试了所有我能想到的东西。

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];

10 个解决方案

#1


17  

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:

如果你正在寻找布局UIBarButtonItems小于默认甚至0垂直间距(像我),你自动完成为你购买UIToolBar;这是我建议的:

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:

UIToolBar私下布局它的项目。苹果工程师永远不会指望开发人员覆盖它。使用固定空格覆盖默认的10Point水平间距是不够的。由于UIToolbar将其项目至少相隔10个点,因此您可以将这些UIBarButtonItems的宽度设置为-10,以便不获取间距,如下面的代码所示:

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]];

#2


5  

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:

如果在每个项目之间添加间隔符,则间距应该自行运行。在您的情况下,您可能希望在两个按钮的两侧放置一个垫片,并在两个按钮之间放置一个垫片。你可以这样做:

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

NSArray *items = [[NSArray alloc] initWithObjects: spacer, barButton1, spacer, barButton2, spacer, nil];

[spacer release];

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.

请注意,您也可以使用UIBarButtonSystemItemFixedSpace,但您需要明确指定它的'width'属性。虽然UIBarButtonSystemItemFlexibleSpace可以为您解决这个问题。

#3


2  

I have solved this problem, you should use flexible item before and after each button.

我已经解决了这个问题,你应该在每个按钮之前和之后使用灵活的项目。

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

Just add the flexibleSpace to your array of items which gonna be set in toolbar.

只需将flexibleSpace添加到您将在工具栏中设置的项目数组中。

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.

附:在摆脱这种情况之前,我几乎已经杀了自己,并尝试了所有类型的解决方案,包括使用其他条形而不是UIToolBar。

#4


2  

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.

神奇的密码是将UIBarButtonItem样式设置为UIBarButtonItemStyleBordered。

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:aView];
item.style = UIBarButtonItemStyleBordered;

Thats it...

而已...

#5


2  

It is possible to add a fixed space item with a negative width between the items to compensate the spacing.

可以在项目之间添加具有负宽度的固定空间项目以补偿间距。

UIBarButtonItem *fixedSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
fixedSpace.width = -12.0f;

#6


2  

In between two bar button you can take the  UIBarButtonSystemItemFixedSpace. And adjust the width.It has worked for me.

在两个条形按钮之间,您可以使用UIBarButtonSystemItemFixedSpace。并调整宽度。它对我有用。

#7


1  

I met the same issue. Finally, I solved this by subclassing UIToolbar and override the layout subviews method.

我遇到了同样的问题。最后,我通过继承UIToolbar并覆盖布局子视图方法解决了这个问题。

- (void)layoutSubviews {

  [super layoutSubviews];

  if (leftItem_ && leftItem_.customView
      && [leftItem_.customView isKindOfClass:[UIButton class]]) {
    CGRect newFrame = leftItem_.customView.frame;
    newFrame.origin.x = 0;   // reset the original point x to 0; default is 12, wired number
    leftItem_.customView.frame = newFrame;    
  }

  if (rightItem_ && rightItem_.customView
      && [rightItem_.customView isKindOfClass:[UIButton class]]) {
    CGRect newFrame = rightItem_.customView.frame;
    newFrame.origin.x = self.frame.size.width - CGRectGetWidth(newFrame);
    rightItem_.customView.frame = newFrame;
  }

}

#8


1  

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.

我能够通过将灵活空间作为最左侧项目,然后将另一个作为最右侧项目,在Xcode4中解决此问题。

enter image description here
enter image description here

#9


0  

UIBarButtonItem *barButton1,*barButton2;

barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image1.png"] style:UIBarButtonItemStylePlain
    target:self action:@selector(action:)];

barButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bart_tb.png"] style:UIBarButtonItemStylePlain
    target:self action:@selector(action:)];

NSArray *items = [[NSArray alloc] initWithObjects: barButton1, barButton2, nil];
[barButton1 release];
[barButton2 release];
[self.toolbar setItems:items animated:NO];
[items release];

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.

这样做..如果你想改变按钮的宽度,设置UIBarButtonItem对象的width属性。 width的默认值为0,这使得它足够大以适合其图像/标题。

Hope that helps.

希望有所帮助。

#10


0  

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.

你可以拿一个新的UIView有相同的号码。您要显示的工具栏上的按钮。然后根据工具栏设置大小。

In short the normal buttons ABOVE the toolbar will look like toolbar buttons.

简而言之,工具栏上方的常规按钮看起来像工具栏按钮。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号