Tôi đã thiết kế chế độ xem với thanh công cụ xuất hiện một cách bình thường trong Trình tạo giao diện. Tôi có một UIBarButtonItem đó là ở phía bên tay trái, mà tôi muốn xuất hiện ở phía bên tay phải của thanh công cụ. Làm thế nào tôi có thể làm điều này trong IB, hoặc thông qua mã?Cách đặt UIBarButtonItem ở bên phải của thanh công cụ UIToolbar?
26
A
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".
-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
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
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
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
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:
Các vấn đề liên quan
- 1. Màu UISegmentedControl trong thanh công cụ UIToolbar
- 2. UIToolbar không hiển thị UIBarButtonItem
- 3. Cách xác định vị trí của UIBarButtonItem trong UIToolbar?
- 4. Thanh công cụ UIPopoverController ở trên cùng
- 5. Đặt Thanh công cụ Các mục của UINavigationController
- 6. Biểu tượng trên thanh công cụ của iPad: hình ảnh nút thanh không ở giữa?
- 7. Thay đổi màu bóng UIToolbar cho UIBarButtonItem
- 8. Cách đặt Expander ToggleButton ở bên phải
- 9. Thêm thanh công cụ UIToolbar để nhập giao diện phụ kiện vào một số trường văn bản
- 10. Cách tạo biểu tượng thanh Notification \ status ở bên phải?
- 11. Cách tô màu nền UIBarButtonItem?
- 12. Chú giải công cụ Highcharts luôn ở bên phải của con trỏ
- 13. cách đặt nền của uitoolbar tương tự như thanh điều hướng của tôi
- 14. Thay đổi văn bản của UILabel (UIBarButtonItem) trên thanh công cụ lập trình
- 15. Dấu phân cách giữa các mục trên thanh công cụ trong UIToolbar
- 16. Làm cách nào để hiển thị nút ở giữa thanh công cụ?
- 17. Eclipse + Git - Cách lấy thanh công cụ?
- 18. Đặt mục nút thanh trên thanh công cụ
- 19. Vị trí UIBarButtonItem ở cạnh của UINavigationBar
- 20. UIToolBar - tắt các nút
- 21. đặt ActionBarDrawerTìm kiếm ở góc bên phải
- 22. Đặt WinForm ở dưới cùng bên phải
- 23. Thêm công cụ chọn tùy chỉnh vào UIBarButtonItem
- 24. Thêm thanh công cụ vào UITableViewController
- 25. Cách đặt chiều cao nút thanh công cụ?
- 26. UISearchBar trong thanh UIToolbar sẽ không hiển thị nút hủy
- 27. Làm thế nào để thêm UIBarButtonItem trong UIToolBar trong mã
- 28. TinyMCE - vị trí thanh công cụ bên ngoài
- 29. Cách tắt nút thanh bên phải của điều hướng
- 30. Tạo thanh công cụ của trình duyệt?
này nghe có vẻ giống như một giải pháp tốt hơn so với tôi! – bpapa
Hoạt động như một sự quyến rũ! Giải pháp tuyệt vời. –
Khi tôi làm điều này, nút không xuất hiện trên màn hình. –