add Toolbar above UITableView for use in UISplitViewController detail view

戏子无情 提交于 2019-12-04 19:19:13

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.

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!