Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change Constraint IBOutlet that is defined for different size classes in IB

I made a special test app for this case. (I'm sorry it is already removed)

I added a view on my controller's view in Storyboard, set up AutoLayout constraints in Interface Builder and made one of them (vertical space) is defferent for different size classes. Screenshot from IB

So the value is 100 for Any height, Any width and 0 for Regular height, Regular width. It works well, on iPhone vertical distance from top is 100, when on iPad it is 0.

Also I made IBOutlet for this constraint and want to change it in runtime to 10

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topVerticalConstraint;

it seemed I couldn't change it because it gives no effect

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.topVerticalConstraint.constant = 10; // it doesn't work
}

Although it works when I remove value for Regular height, Regular width in Interface Builder.

Am I miss something about the size classes?

like image 737
Sander Avatar asked Jun 05 '15 09:06

Sander


2 Answers

The problem is that constraints are not fully defined yet until Layout events happen between -viewWillLayoutSubviews and -viewDidLayoutSubviews where all the parameters from IB comes into play. My rule of thumb is:

  • if you use frames to position your views manually you can do it as early as -viewDidLoad,
  • if you use autolayout constraints for positioning, make adjustments as early as -viewDidLayoutSubviews;

The second statements only considers code adjustments to constraints that have been made in IB. Adjustments that you are making in -viewDidLoad will be overridden by parameters set in IB during layout. If you add constraints with code you can set them in -viewDidLoad, since there will be nothing to override them.

I've changed your code a bit and it works:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topVerticalConstraint;
@property (weak, nonatomic) IBOutlet UIView *square;

@property (assign, nonatomic) BOOL firstLayout;

@end

@implementation ViewController

- (void)viewDidLoad

{
    [super viewDidLoad];

    self.firstLayout = YES;
}

- (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    if (self.firstLayout) {

        self.topVerticalConstraint.constant = 10;
        self.firstLayout = NO;

    }
}

@end

Notice that -viewDidLayoutSubviews is called many times during the lifetime of a ViewController, so you have to make sure that your adjustments happen only once on initial load.

like image 174
NKorotkov Avatar answered Sep 20 '22 15:09

NKorotkov


The problem:

If you set up different value for different size classes in IB for the constraint like this:

enter image description here

then you can't change constant value in code like this:

self.adHeightConstraint.constant = 0;  // value set to 0
[self.view layoutIfNeeded];  // value get back to IB value (44 or 36)

In this situation you may see that your constant value persists only until views recalculates. So, after [self.view layoutIfNeeded] the value of constant reset back to whatever was set in IB.

The solution:

  1. Add the second constraint of the same attribute (in my case it was the height) with desired value. You may set this value in IB or change it in the code.
  2. Set low priority for this new constraint. Since it's low priority, it won't be any conflict.
  3. Now when you need to apply the new constant, simple disable the first constraint:

    self.adHeightConstraint.active = NO;
    [self.view layoutIfNeeded];
    
like image 31
Mike Keskinov Avatar answered Sep 20 '22 15:09

Mike Keskinov