Search code examples
iphoneuitoolbar

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

Solution

  • 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...