Is it possible to put center a button on an UIToolBar on iPhone?
I want to put a button on UIToolBar at the center position. What changes do I have to make in the following code?
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];
I think if you create two UIBarButtonItems, using initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace, and put one before your button, and the other after -- you'll get the centering that you desire...
Create one bar button of UIBarButtonSystemItemFlexibleSpace
type.
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];
精彩评论