How to add a UIView above the current UITableViewController

后端 未结 20 736
旧时难觅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:35

    I tried the approach above, but did not get it to work. I also found it to require too much configuration and code, since it requires setting up the table view from scratch (something that is easily done from within the storyboard).

    Instead, I added the view that I wanted to add above my UITableView into the UITableViewController's UINavigationController's view, as such:

    [self.navigationController.view addSubview:<view to add above the table view>];
    

    This approach requires that you have embedded the UITableViewController in a UINavigationController, but even if you do not want a navigation controller, you can still use this approach and just hide the navigation bar.

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

    I added a image above the the table. I used this code:

    self.tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header.png"]];
    
    0 讨论(0)
  • 2020-11-27 12:37

    To keep UIView above table view in UITableViewController I'm using one(or more) of delegate methods (UITableViewDelegate).

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.addSubview(headerView)
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        tableView.addSubview(overlayView) // adds view always on top
    }
    // if using footers in table view
    tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { ... }
    

    As views can only have one superview that seams too be good solution, correct me if I'm wrong. Still getting 60fps so it's fine for me.

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

    I had a similar problem where I wanted to add a loading indicator on top of my UITableViewController. To solve this, I added my UIView as a subview of the window. That solved the problem. This is how I did it.

    -(void)viewDidLoad{
        [super viewDidLoad];
        //get the app delegate
        XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    
        //define the position of the rect based on the screen bounds
        CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
        //create the custom view. The custom view is a property of the VIewController
        self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
        //use the delegate's window object to add the custom view on top of the view controller
        [delegate.window addSubview: loadingView]; 
    }
    
    0 讨论(0)
  • 2020-11-27 12:41

    You can increase the zPosition of the layer of your view.

    This will make it display above the other views (which have a zPosition equal to 0, by default)

    self.progView.layer.zPosition++;
    [self.view addSubview:self.progView];
    
    0 讨论(0)
  • 2020-11-27 12:41

    I had similar problem and got it solved using below code :

         [self.navigationController.view insertSubview:<subview> 
           belowSubview:self.navigationController.navigationBar];
    

    This inserts view in correct place using controllers present in Stack.

    0 讨论(0)
提交回复
热议问题