回答

57

這裏是如何做到這一點的代碼,如果有人遇到這個帖子:

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


self.toolbarItems = [NSArray arrayWithObjects: leftButton, flex, rightButton, nil]; 
29

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

+0

這聽起來似乎比我更好的解決方案! – bpapa 2010-03-22 15:58:49

+0

工程就像一個魅力!好的解決方案 – 2010-03-22 16:04:02

+0

當我這樣做時,該按鈕不會出現在屏幕上。 – 2016-07-12 19:44:06

-1

如果你能逃脫這不是一個的UIBarButtonItem(例如,信息按鈕),我做的是改變按鈕的層總是在工具欄的前面。這樣,當我在IB中的工具欄上拖動按鈕時,它不會自動將它吸入到左側。

8

Swift代碼:

func addDoneButton() -> UIToolbar { 
    let toolbar = UIToolbar() 
    let flexButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: Selector("donePressed")) 
    toolbar.setItems([flexButton, doneButton], animated: true) 
    toolbar.sizeToFit() 
    return toolbar 
} 
27

- 在這樣

enter image description here正確的項目

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1」 style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: flexible, item1, nil]; 

- 兩個項目左,右像這樣

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1」 style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2」 style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, nil]; 

三防項目這樣

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1」 style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2」 style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3」 style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil]; 

- 四個項目這樣

enter image description here

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

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1」 style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2」 style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"item3」 style:UIBarButtonItemStylePlain target:self action:nil]; 
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithTitle:@"item4」 style:UIBarButtonItemStylePlain target:self action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, item3, flexible, item4, nil]; 

所以,當你想添加一個空格,你必須添加柔性杆按鈕。

0

斯威夫特3.x或以上:

internal var textFieldHandlerToolBar: UIToolbar = { 
     let tb = UIToolbar.init(frame: CGRect.init(origin: .zero, size: CGSize.init(width: UIScreen.screenSize().width, height: 44.0))) 
     let flexibleButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 
     let doneBarButton = UIBarButtonItem.init(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(actionDonePickerSelection)) 
     tb.setItems([flexibleButton, doneBarButton], animated: false) 
     return tb 
    }() 

輸出:

enter image description here

相關問題