我正在尝试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];