Changing NSLayoutContraint constant in viewDidLoad or viewWillAppear does not work

I am trying to do something like this:

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"%@", self.tableViewHeight); self.tableViewHeight.constant = 0; NSLog(@"%@", self.tableViewHeight); [self.tableView setNeedsUpdateConstraints]; [self.tableView setNeedsLayout]; [self.view setNeedsUpdateConstraints]; [self.view setNeedsLayout]; } 

But sometimes this work, and sometimes not. I always see the correct log messages:

 <NSLayoutConstraint:0x9ebe7a0 V:[UITableView:0xa345a00( 304@500 )] priority:500> <NSLayoutConstraint:0x9ebe7a0 V:[UITableView:0xa345a00( 0@500 )] priority:500> 

Why is this happening? How to change the NSLayoutConstraints properties before the user interface becomes visible?

UPDATE:

This code works 20 times by 20 times (but I think it is always always):

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; dispatch_async(dispatch_get_main_queue(), ^{ self.tableViewHeight.constant = 0; [self.view setNeedsUpdateConstraints]; }); } 

Just tell me why this is actually happening?

UPDATE 2:

It seems that the previous code still does not work, but it works:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.topViewHeight.constant += self.tableViewHeight.constant; self.tableViewHeight.constant = 0; [self.view setNeedsUpdateConstraints]; } 

UPDATE 3:

I NEVER SET EQUAL PRIORITIES (AS LIKE 500) TO SEVERAL RESTRICTIONS WITH CONNECTED VIEWS ...

+4
source share
1 answer

I noticed that you have a priority of 500, which means that you are ready to violate this restriction in order to satisfy other restrictions. I wonder what other restrictions you have. If you do this in Interface Builder, it has an annoying tendency (at least in Xcode 4.6.x) to add any restrictions that, in its opinion, are needed to eliminate the ambiguity. You might want to check that you have no other height restrictions, or upper and lower limits that take precedence.

On the bottom line, your technique for setting constant is fine (I think I think just using setNeedsLayout is sufficient), and the problem is probably related to other restrictions that may be delayed.

If you change the priority of this restriction to 1000, if you have conflicting restrictions, you will get an error regarding the fact that they are, which is useful in diagnosing the problem. But be that as it may, you are telling autostart that this is normal if this height restriction is ignored in favor of other conflicting restrictions (greater or equal priority). By setting the priority to 1000, you say that you never want this restriction to be ignored.

+1
source

Source: https://habr.com/ru/post/1488729/


All Articles