How to add a UIView above the current UITableViewController

后端 未结 20 733
旧时难觅i
旧时难觅i 2020-11-27 11:57

I have difficulty adding a subview (UIView) from within the viewDidLoad method of a UITableViewController

This works:

[self.view addSubview:self.prog         


        
相关标签:
20条回答
  • 2020-11-27 12:29

    The Apple example "iPhoneCoreDataRecipes" is using a NIB to load a header onto a UITableView. See here:

    0 讨论(0)
  • 2020-11-27 12:30

    Update for iOS 11:

    It is now possible to add an Subview to UITableView when using AutoLayout constraints to the safe area. These Views will not scroll along the TableView.

    This example places a view below the NavigationBar on top of the UITableView of a UITableViewController

    [self.tableView addSubview:self.topBarView];
    [NSLayoutConstraint activateConstraints:@[
        [self.topBarView.topAnchor constraintEqualToAnchor:self.tableView.safeAreaLayoutGuide.topAnchor],
        [self.topBarView.leadingAnchor constraintEqualToAnchor:self.tableView.safeAreaLayoutGuide.leadingAnchor],
        [self.topBarView.trailingAnchor constraintEqualToAnchor:self.tableView.safeAreaLayoutGuide.trailingAnchor],
        [self.topBarView.heightAnchor constraintEqualToConstant:40.0]
    ]];
    
    0 讨论(0)
  • 2020-11-27 12:32

    The problem is that the view property of UITableViewController is identical to the tableView property. What this means is that the root view is always a table view controller, and anything added as a subview will be subject to the table view functionality. This has other undesirable side effects, like your subviews scrolling when you may not want them to.

    There are a couple options here. You could override loadView and install your own view and table view:

    // note: untested
    - (void)loadView {
        self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        self.view.backgroundColor = [UIColor whiteColor];
    
        UITableView *tblView = [[UITableView alloc]
            initWithFrame:CGRectZero
            style:UITableViewStylePlain
        ];
    
        tblView.autoresizingMask =
            UIViewAutoresizingFlexibleWidth |
            UIViewAutoresizingFlexibleHeight
        ;
    
        self.tableView = tblView;
        [self.view addSubview:tblView];
        [tblView release];
    }
    

    And then when you need to add a subview, add it below or above self.tableView as appropriate.

    Another option is just to create a UIViewController subclass that does what you need. UITableViewController honestly doesn't add that much, and the little functionality it does implement is easily replicated. There are articles like Recreating UITableViewController to increase code reuse that explain how to do this pretty easily.

    0 讨论(0)
  • 2020-11-27 12:33

    try something like this:

    [self.tableView addSubview:overlayView];
    overlayView.layer.zPosition = self.tableView.backgroundView.layer.zPosition + 1;
    
    0 讨论(0)
  • 2020-11-27 12:34

    Swift 2018

    Here is my way with storyboard:

    1) Add a view in storyboard.

    2) Link it with UITableViewController class:

    @IBOutlet weak var copyrightLabel: UILabel!
    

    3) Add it in code

    self.navigationController?.view.addSubview(copyrightView)
    copyrightView.frame = CGRect(x: 0,
                                y: self.view.bounds.size.height - copyrightView.bounds.size.height,
                                width: self.view.bounds.size.width,
                                height: copyrightView.bounds.size.height)
    

    4) Voilla!

    The view will not scroll with the table view. It can be easy designable from the storyboard.

    NOTE: This solution adds subview to the navigation controller and if you are going to another screen from here further down the nav, you will find this subview to persist, remove it using copyrightView.removeFromSuperView on viewDidDisappear while performing segue.

    0 讨论(0)
  • 2020-11-27 12:34

    As UITableViewController is a subclass of UIViewController, you need to add your desired view to its superview.

    Swift:
    self.view.superview?.addSubview(viewTobeAdded)
    
    Objective C:
    [self.view.superview addSubview: viewTobeAdded];
    
    0 讨论(0)
提交回复
热议问题