Cách đơn giản nhất để thêm UIToolBar vào UITableViewController là gì? Tôi phụ thuộc vào chức năng chỉnh sửa, vì vậy tôi không thể thay đổi UITableViewController thành UIViewController dễ dàng.Thêm thanh công cụ vào UITableViewController
Trả lời
Không sao cả, UITableViewController
là phân lớp của UIViewController
. Và điều đó xảy ra trong hệ điều hành iPhone 3.0 bất kỳ UIViewController
(và các lớp con) có thể hoạt động cùng với UINavigationController
để cung cấp thanh công cụ nhận biết ngữ cảnh.
Để cho tiện làm việc bạn phải:
- Hãy chắc chắn rằng bạn sử dụng một
UINavigationController
để chứa tất cả các bộ điều khiển xem bạn cần một thanh công cụ. - Đặt thuộc tính
toolbarsItems
của trình điều khiển chế độ xem muốn có thanh công cụ.
Điều này gần như dễ dàng như đặt tiêu đề của bộ điều khiển chế độ xem và phải được thực hiện theo cách tương tự. Hầu hết có thể bằng cách ghi đè trình khởi tạo initWithNibName:bundle:
. Như một ví dụ:
-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
self = [super initWithNibName:name bundle:bundle];
if (self) {
self.title = @"My Title";
NSArray* toolbarItems = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addStuff:)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchStuff:)],
nil];
[toolbarItems makeObjectsPerformSelector:@selector(release)];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
}
return self;
}
Bạn cũng có thể sử dụng setToolbarItems:animated:
thay vì gán cho toolbarItems
tài sản, thêm và xoá các mục thanh công cụ trong một thời trang hoạt hình một cách nhanh chóng.
Để thực hiện công thức PeyloW của để làm việc, tôi cần phải thêm dòng thêm mã sau đây:
self.navigationController.toolbarHidden = NO;
Hy vọng rằng sẽ giúp ...
Đồng ý. Tôi đã phải thực hiện cuộc gọi đó trong phương thức viewDidLoad, chứ không phải ghi đè initWithNibName. Sau đó, nó hoạt động tuyệt vời. –
bạn vừa lưu lại ngày của tôi, cảm ơn bạn –
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];
}
- (void) info_clicked:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];
}
Và trong Swift 3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Initialize the toolbar
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
//Set the toolbar to fit the width of the app.
toolbar.sizeToFit()
//Caclulate the height of the toolbar
let toolbarHeight = toolbar.frame.size.height
//Get the bounds of the parent view
let rootViewBounds = self.parent?.view.bounds
//Get the height of the parent view.
let rootViewHeight = rootViewBounds?.height
//Get the width of the parent view,
let rootViewWidth = rootViewBounds?.width
//Create a rectangle for the toolbar
let rectArea = CGRect(x: 0, y: rootViewHeight! - toolbarHeight, width: rootViewWidth!, height: toolbarHeight)
//Reposition and resize the receiver
toolbar.frame = rectArea
//Create a button
let infoButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(infoClicked))
toolbar.items = [infoButton]
//Add the toolbar as a subview to the navigation controller.
self.navigationController?.view.addSubview(toolbar)
}
func infoClicked() {
//Handle Click Here
}
Điều này làm việc tuyệt vời cho tôi. Tôi không thể thêm một 'UINavigationController', do đó, thêm một thanh công cụ là cách duy nhất để đi một cách thủ công. Cảm ơn! – codingFriend1
Tuyệt. Tôi nghĩ rằng đây sẽ là câu trả lời được chấp nhận. Tôi muốn ** thêm một thanh công cụ để uitableviewcontroller **, không cho phép uinavigationcontroller. – soemarko
- 1. Visual Studio 2010: Thêm lệnh vào thanh công cụ
- 2. Thêm mục vào thanh công cụ Tìm kiếm
- 3. Thêm nút vào jqGrid thanh công cụ trên cùng
- 4. Thêm các nút vào thanh công cụ của Finder?
- 5. Thêm/thêm lại các nút vào thanh công cụ trong nhật thực?
- 6. Thêm công cụ chọn tùy chỉnh vào UIBarButtonItem
- 7. Làm cách nào để thêm các nút vào thanh công cụ Eclipse?
- 8. Làm cách nào để thêm các nút vào thanh công cụ FireMonkey?
- 9. Làm cách nào để thêm QComboBox vào Thanh công cụ chính trong Qt Creator
- 10. Làm thế nào để thêm lập trình NSToolbarItem mới vào thanh công cụ hiện có?
- 11. Làm cách nào để thêm thanh công cụ vào cuối một NSTableView?
- 12. Cách thêm nút "Đang chờ thay đổi" vào thanh công cụ trong VS 2010
- 13. Thêm thanh công cụ tùy chỉnh vào CKEditor trong đường ray
- 14. Thêm jqGrid Điều hướng Tùy chỉnh vào Thanh công cụ Hàng đầu
- 15. Thêm nút "Chọn từ Thư viện ảnh" vào thanh công cụ của UIImagePickerController
- 16. Magento thêm thanh công cụ của máy nhắn tin vào danh sách yêu thích
- 17. Thêm UIView vào sau UITableView trong mã UITableViewController
- 18. Eclipse + Git - Cách lấy thanh công cụ?
- 19. Thêm tổng giá vào trang thanh toán thành công
- 20. Thanh công cụ thiếu ALV Lưới
- 21. Ẩn thanh công cụ Adobe, thanh menu, thanh cuộn, v.v.
- 22. iOS - Thanh công cụ không hiển thị
- 23. Làm cách nào để tạo thanh công cụ trông giống như thanh công cụ trong Keynote?
- 24. Cách thêm chú giải công cụ vào đồ họa svg?
- 25. Cách thêm chú giải công cụ vào biểu đồ chart.js
- 26. Thêm chú giải công cụ vào một CStatic
- 27. Cách thêm chú giải công cụ vào ô trong jtable?
- 28. Thêm công cụ chọn phương thức vào từ điển
- 29. Người mới bắt đầu: Cách thêm thanh trên cùng bằng các nút trên UITableViewController
- 30. Thanh công cụ ExtJS có nhiều hàng
Yêu cầu NavigationController phải không? Tôi muốn thêm một ToolBar vào một TableViewController mà không phải là một phần của một NavigationController. Tôi có cần phải sử dụng một NavigationController mặc dù sẽ chỉ có một lần xem trong nó? –
@sirjorj Có 'UINavigationController' là bắt buộc để xử lý thanh công cụ * miễn phí *. Không có nó, bạn phải quản lý cá thể 'UIToolbar' của riêng bạn. – PeyloW
nếu tôi không muốn đặt các nút trên thanh công cụ này, thay vào đó, tôi chỉ muốn đặt một hình ảnh ở giữa, tôi sẽ làm gì khác? Cảm ơn. –