vendredi 14 août 2015

Strange navigation controller behavior

I have a view that is pushed onto navigation stack like this:

FriendsDetailViewController *detail = [[FriendsDetailViewController alloc] init];
detail.user = selectedUser;
[self.navigationController pushViewController:detail animated:YES];

Inside a view controller, I have two elements: my custom controls view, that is a view with two buttons and a label inside, and a table view. I am setting constraints to them as shown:

-(void)setupView {
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.tableView];


    self.controlsView = [[ControlsView alloc] init];
    self.controlsView.player = self.player;
    self.controlsView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.controlsView];
    [self setControlsViewConstraints];
    [self setTableViewConstraints];
}

-(void)setTableViewConstraints {
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.controlsView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
    NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.view addConstraints:@[topConstraint, leadingConstraint, trailingConstraint, bottomConstraint]];
}

-(void)setControlsViewConstraints {
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
    NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.controlsView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];
    [self.view addConstraints:@[top, leading, trailing, height]];
}

But by the end I get unexpected result.

enter image description here

Firstly, my custom controls view is black, although in code the background color is set to white. Secondly, custom controls view is situated just as I expected, but my table View is messed up. Somehow it does not sit on the bottom of my controls view.

I have other view controller without an embedded navigation controller, and the layout is just fine. enter image description here

It seems like I don't catch how navigation view is embedded in my view controller. I do the whole project without Interface builder, and this strange behavior is really confusing.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire