Trả lời

49

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.

+0

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ó? –

+0

@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

+0

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. –

39

Để 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 ...

+2

Đồ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. –

+0

bạn vừa lưu lại ngày của tôi, cảm ơn bạn –

12
- (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 
} 
+0

Đ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

+1

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

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