2010-11-22 7 views
1

Ich möchte einen Knopf auf UIToolBar in der Mitte Position setzen. Welche Änderungen muss ich im folgenden Code vornehmen?Ist es möglich, eine Taste auf einer UIToolBar auf dem iPhone zu zentrieren?

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

Antwort

5

Ich denke, wenn man zwei UIBarButtonItems erstellen, mit initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace, und legte eine vor Ihrer Taste, und die andere nach - Sie werden die Zentrierung erhalten, dass Sie sich wünschen ...

+0

Danke! Das hat mir geholfen. –

1

erstellen eine Bar-Taste von UIBarButtonSystemItemFlexibleSpace Typ.

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