2012-10-13 141 views
12

我創建了一個工具欄程序:如何在工具欄的右側添加按鈕?

UIToolbar *boolbar = [UIToolbar new]; 
    boolbar.barStyle = UIBarStyleDefault; 
    boolbar.tintColor = [UIColor orangeColor]; 
    [boolbar sizeToFit]; 

,然後添加一個按鈕,它:

UIBarButtonItem *cancelleftBarButton =[[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(tapBackGround:)]; 

cancelleftBarButton.tintColor = [UIColor orangeColor]; 

NSArray *array = [NSArray arrayWithObjects:cancelleftBarButton, nil]; 
[boolbar setItems:array animated:YES]; 

然而,此按鈕僅在工具欄的左側。是否有可能把它放在工具欄的右側?

enter image description here

回答

34

這裏是一個工具欄的右側添加UIBarButtonItem的方法。

UIBarButtonItem *leftButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem1Pressed:)] autorelease]; 

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease]; 

UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(btnItem2Pressed:)] autorelease]; 

OR

如果你正試圖從廈門國際銀行這樣做,那麼。

插入標識符爲「靈活空間」的項目。

enter image description here

+0

還可以點擊這些鏈接http://stackoverflow.com/questions/602717/aligning-uitoolbar-items和http://stackoverflow.com/問題/ 6021138 /如何做調整 - uitoolbar - 左 - 右 - 襯墊 – IronManGill

4

在斯威夫特

let btn1 = UIBarButtonItem(title: "Button 1", style: UIBarButtonItemStyle.Done, target: self, action: "btn1Pressed" 
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
let btn2 = UIBarButtonItem(title: "Button 2", style: UIBarButtonItemStyle.Done, target: self, action: "btn2Pressed") 
相關問題