12

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

非常感谢您的帮助。

下面是一些关于我将如何处理的示例代码:

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

10 回答 10

17

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

UIToolBar 私下布局它的项目。Apple 工程师永远不会真正期望开发人员覆盖它。使用固定空格覆盖默认的 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]];
于 2011-10-09T10:49:53.253 回答
5

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

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

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

[spacer release];

请注意,您也可以使用 UIBarButtonSystemItemFixedSpace 但您需要明确指定它的“宽度”属性。而 UIBarButtonSystemItemFlexibleSpace 似乎可以为您解决这个问题。

于 2009-11-25T03:01:50.840 回答
2

好吧,这是一个非常古老的问题,但我刚刚遇到了同样的问题并设法解决了它。

魔术密码将 UIBarButtonItem 样式设置为 UIBarButtonItemStyleBordered。

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

而已...

于 2011-02-15T23:35:41.550 回答
2

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

UIBarButtonItem *fixedSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
fixedSpace.width = -12.0f;
于 2011-06-22T19:19:31.247 回答
2

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

于 2012-02-02T07:47:15.250 回答
2

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

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

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

ps 在摆脱这个之前我几乎自杀了,并尝试了所有类型的解决方案,包括使用其他栏而不是 UIToolBar。

于 2010-09-16T07:07:59.937 回答
1

我遇到了同样的问题。最后,我通过继承 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;
  }

}
于 2011-08-25T05:56:06.173 回答
1

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

在此处输入图像描述
在此处输入图像描述

于 2012-01-04T18:12:35.520 回答
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];

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

希望有帮助。

于 2009-08-02T22:16:23.203 回答
0

我今天遇到了同样的问题,用简单的方法解决了,所以分享一下。

您可以采用具有相同编号的新 UIView。您要显示的工具栏上的按钮。然后只需根据工具栏设置大小。

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

于 2013-09-16T11:15:50.390 回答