2010-11-22 14 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

あなたがinitWithBarButtonSystemItem使用して、2 UIBarButtonItemsを作成した場合、私は思う:UIBarButtonSystemItemFlexibleSpaceを、そしてあなたのボタンの前に1を入れて、後に他の - ...あなたが望むことを中心に取得しますが

+0

ありがとうございます!それが私を助けました。 –

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]; 
関連する問題