我想在UIToolBar的中心位置放一个按钮。我需要在以下代码中进行哪些更改?
CGRect toolbarFrame = CGRectMake(0, 0, self.view.frame.size.width, 44);
UIToolbar *mytoolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
mytoolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
mytoolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"button 1"
style:UIBarButtonItemStylePlain target:self action:nil];
NSMutableArray *tools = [[NSMutableArray alloc] initWithObjects:button,nil];
[mytoolbar setItems:tools];
[self.view addSubview:mytoolbar];
答案 0 :(得分:5)
我想如果你创建两个UIBarButtonItems,使用initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace,并在你的按钮之前放一个,然后另一个 - 你会得到你想要的中心...
答案 1 :(得分:1)
创建一个UIBarButtonSystemItemFlexibleSpace
类型的条形按钮。
UIBarButtonItem *spaceButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain
target:self action:nil];
NSMutableArray *tools =
[[NSMutableArray alloc] initWithObjects:spaceButton, button, spaceButton,nil];
[mytoolbar setItems:tools];