Trả lời

57

Dưới đây là làm thế nào để làm điều đó trong mã nếu có ai đi qua bài viết này:

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

Chèn mục có số nhận dạng là "không gian linh hoạt".

+0

này nghe có vẻ giống như một giải pháp tốt hơn so với tôi! – bpapa

+0

Hoạt động như một sự quyến rũ! Giải pháp tuyệt vời. –

+0

Khi tôi làm điều này, nút không xuất hiện trên màn hình. –

-1

Nếu bạn có thể nhận được ngay với điều này không phải là một UIBarButtonItem (ví dụ, một nút thông tin), những gì tôi làm là thay đổi các lớp nút để luôn luôn ở phía trước của Thanh công cụ. Bằng cách này, khi tôi kéo nút trên thanh công cụ trong IB nó không tự động hút nó vào và sang phía bên trái.

8

Swift Code:

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

- một mục trong quyền như thế này

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

-Hai mặt hàng trái và phải như thế này

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

mục -Ba như thế này

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

-Four mục như thế này

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

vì vậy khi bạn muốn thêm một không gian bạn phải thêm nút thanh linh hoạt.

0

Swift 3.x trở lên:

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 
    }() 

Output:

enter image description here

Các vấn đề liên quan