Adding a subview to a view controller messes up with a table view

109 views Asked by At

I have a UIViewController that has a UITableView as a subview. I want to add a background view under the table view but when I add it, the controller's edgesForExtendedLayout seem to be messed up

This is correct: enter image description here

This is not correct: enter image description here

My viewDidLoad method looks like this. If I do not add the subview, all looks ok.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // UITableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TAManeuvreCellId];

    [self.view addSubview:self.tableView];


    UIView *backgroundBlurView = [[UIView alloc] init];


    // autolayout
    backgroundBlurView.translatesAutoresizingMaskIntoConstraints = NO;

    NSDictionary *views = NSDictionaryOfVariableBindings(backgroundBlurView, _tableView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]|" options:0 metrics:nil views:views]];

    // comment this and everything is ok
    [self.view addSubview:backgroundBlurView];
    [self.view sendSubviewToBack:backgroundBlurView];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[backgroundBlurView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[backgroundBlurView]|" options:0 metrics:nil views:views]];
}
1

There are 1 answers

5
keji On

I think the fix for this is setting:

self.navigationController.navigationBar.translucent = FALSE;
self.edgesForExtendedLayout = UIRectEdgeNone;

See:

How to prevent UINavigationBar from covering top of view in iOS 7?

iOS 7 UIImagePickerController navigationbar overlap