2010-11-22 103 views
1

我想在UIToolBar的中心位置放一个按钮。在下面的代码中我必须做什么改变?是否可以在iPhone上的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]; 

回答

5

我觉得如果你创建了两个UIBarButtonItems,使用initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace,并把你的一个按钮之前,和之后的另 - 你会得到你想要的中心......

+0

谢谢!这帮助了我。 – 2010-12-13 01:19:09

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]; 
相关问题