add Toolbar above UITableView for use in UISplitVi

2019-04-16 15:54发布

问题:

I want to use a table view as the detail pane in my UISplitViewController. How do I put the toolbar at the top? I want to be able to add bar button items in the same way as my non-table detail views. Thanks.

回答1:

My frustration with this problem lay in trying to use the UITableViewController class, which does not allow you to add other UI elements like a toolbar. I solved it by creating a UIViewController object and adding the toolbar and table view to its nib individually. I then had the ViewController implement the table view's delegate and data source methods. Works great.



回答2:

common paradigm is have UINavigationControllers as the top level controllers for master and detail pages.

So the view hiearchy looks like this (loosly speaking)

  • Application Window
    • UISplitViewController
      • master : UINavigationController
        • has your custom Controller (tableView or UIView)
      • detail : UINavigationController
        • has your custom controller (UITableViewCOntroller / UIVIEWcontroller)

hope this crude diagram makes sense.

THe perk of having UINavigationController as the top level controller, you get the Toolbar for 'free'.

   self.navigationController.toolbar


回答3:

I solved this other way: in case of absense of navigationBar I just add a toolbar at the top of tableView and I change the height for header in first section. The only problem is that toolbar scrolls with the tableView.

Add this to ViewDidLoad of your TableViewController

if (! self.navigationController.navigationBar) {
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    [self.tableView addSubview:toolBar];
}

Add this method:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section
{
    return 50;
}