Ham Dong Kyun
Ham Dong Kyun

Reputation: 776

Where should the constraints code need to written?

I have written following code in the awakeFromNib.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CalendarMonthBackground"]];
[self addSubview:imageView];

[imageView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:NSLayoutAttributeNotAnAttribute
                                                     multiplier:1.0f
                                                       constant:159.0f]]; // 318 / 2

[imageView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:NSLayoutAttributeNotAnAttribute
                                                     multiplier:1.0f
                                                       constant:122.5f]]; // 245 / 2

[self addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self                                                     
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0f
                                                  constant:0.0]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                 attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeTop
                                                multiplier:1.0f
                                                  constant:-5.0f]];

The constraint is not shown as designed.

Am I setting the constraints code in the wrong place?

I see a lot of samples putting the constraints in the controller, but I want to put it in the view.

Upvotes: 0

Views: 68

Answers (1)

matt
matt

Reputation: 535232

The usual place to update your constraints for a UIView subclass (and this is not terribly surprising, given the name) is... wait for it... updateConstraints!

If you only need to do this once, then use a BOOL instance variable to ensure that.

However, putting your code there is no guarantee that the constraints themselves are good. "The constraint is not shown as designed" could mean anything, so it's hard to help you with any precision. But remember, there are two kinds of mistake you can make - you can create conflicting constraints, or you can create insufficient (ambiguous) constraints. In the latter case you won't get any warning from the runtime; usually, the sign of this is that your views don't appear at all.

What is actually wrong with your code is that you forgot to say:

 imageView.translatesAutoresizingMaskIntoConstraints = NO;

But you see, you made a false assumption (I'm setting my constraints in the wrong place), so you asked the wrong question, so you naturally are getting the wrong answer. Not only that - Xcode was showing you a warning explaining the problem to you (in the console): you are creating conflicting constraints. But you ignored that warning, even though it told you the answer.

Upvotes: 1

Related Questions