12 iphone user-interface uitoolbar
我正在尝试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];
Run Code Online (Sandbox Code Playgroud)
amo*_*sel 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]];
Run Code Online (Sandbox Code Playgroud)
如果在每个项目之间添加间隔符,则间距应该自行运行.在您的情况下,您可能希望在两个按钮的两侧放置一个垫片,一个放在两者之间.你可以这样做:
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *items = [[NSArray alloc] initWithObjects: spacer, barButton1, spacer, barButton2, spacer, nil];
[spacer release];
Run Code Online (Sandbox Code Playgroud)
请注意,您也可以使用UIBarButtonSystemItemFixedSpace,但您需要明确指定它的'width'属性.虽然UIBarButtonSystemItemFlexibleSpace可以为您解决这个问题.
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];
Run Code Online (Sandbox Code Playgroud)
这样做......如果您想更改按钮的宽度,请设置 UIBarButtonItem 对象的 width 属性。宽度的默认值为 0,这使得它足够大以适合其图像/标题。
希望有帮助。
| 归档时间: |
|
| 查看次数: |
10306 次 |
| 最近记录: |