iosautolayoutlayout-anchor

ios setting anchor from XIB table not working properly


I have some widgets defined in an XIB, I want to set the constrains-anchors programatically. When I set an imageView seems to work, but a UITable is disappearing when left, right or bottom anchors are set.

Also the table is not setting Y position underneath the imageView...

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

    [self setupConstrains];
}

- (void)setupConstrains {

//    //image world
    self.whatDoppelsView.translatesAutoresizingMaskIntoConstraints = false;

    [self.whatDoppelsView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
    [self.whatDoppelsView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;

    //table,
    self.table.translatesAutoresizingMaskIntoConstraints = false;
//    
    [self.table.topAnchor constraintEqualToAnchor:self.whatDoppelsView.bottomAnchor constant:0];
//
//    [self.table.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:1];
//
//    //[self.table.heightAnchor constraintEqualToConstant:900];
//    
    [self.table.heightAnchor constraintEqualToAnchor:self.view.heightAnchor multiplier:0.50];

//    [self.table.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
//    [self.table.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;

//    [self.table.bottomAnchor constraintEqualToAnchor: self.view.bottomAnchor].active = YES;

}

If i uncomment the table leading, trailing or bottom anchor, it disappears, also the table should be underneath my imageView.

How to fix this layout?, thanks!


Solution

  • Activate your constraints as you have done in your last lines. Otherwise you just give the constraints to the component and they are not in activate state then nothing happen i.e. components behave like they have no constraints.

    Note:

    Whenever you change the constraints on any component programatically like for some effect now you wanted to your change component layout, then be ensure that you firstly remove all the previous constraints properly, even when you wanted to update any single constraint then first you have to remove perfectly all the previous constraints, otherwise x-code get conflicts.