add Toolbar above UITableView for use in UISplitViewController detail view

Deadly 提交于 2020-01-13 06:43:49

问题


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;
}


来源:https://stackoverflow.com/questions/3105304/add-toolbar-above-uitableview-for-use-in-uisplitviewcontroller-detail-view

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