How to add a UIView above the current UITableViewController

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

    Ive been able to add a subview on top of a uitableviewcontroller by using uiviewcontroller containment.

    UITableViewController is actually very handy when it comes to static cells and this is probably the only time where the common answer "just use uitableview" may actually not viable.

    So this is how I do it.

    1. give your UITableViewController a StoryBoard identifier i.e. MyStaticTableView
    2. create a brand new UIViewController subclass and call it UITableViewControllerContainer
    3. place this controller in place of your UITableViewController inside your storyboard
    4. add a subview to the new controller and link it to an outlet called like "view_container"
    5. on you UITableViewControllerContainer viewDidLoad method

    add code like:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UITableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyStaticTableView"];
        [self addChildViewController:vc];
        [self.view_container addSubview:vc.view];
    }
    

    Problems you may have:

    1. if you have extra top space then be sure to add the flag "wants fullscreen" to your UITableViewController
    2. if it doesn't resize properly on your UITableViewControllerContainer

    add code:

    - (void)viewWillAppear:(BOOL)animated
    {
        [[self.view_container.subviews lastObject] setFrame:self.view.frame];
    }
    

    at this point from your UITableViewController you can access you container view directly with

    self.view.superview.superview

    and whatever you add to it will be show on top your table view controller

提交回复
热议问题