0

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]];
}
Jan
  • 7,444
  • 9
  • 50
  • 74

1 Answers1

0

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

Community
  • 1
  • 1
keji
  • 5,947
  • 3
  • 31
  • 47
  • The thing is that I want my navigation bar to be translucent with extended layout to achieve a blur effect on my content. – Jan Jun 09 '15 at 05:41
  • If you want that than you would need to offset the frame.orgin.y and decrease the height of the tableview by that offset. – keji Jun 09 '15 at 06:08
  • You may also have an option to use autolayout to set the top edge of the tableview to the top layout guideline of the controller. – keji Jun 09 '15 at 06:18
  • So when the nav bar in translucent, the table view has to end under the topLayoutGuide instead of taking the entire controller's view ? – Jan Jun 12 '15 at 06:40
  • If the table view ends just under the `topLayoutGuide` then the nav bar blur is not applied on the table cells – Jan Jul 07 '15 at 18:08