0

I'm trying to make constraints programmatically. I have textField and button created in IB. Here is the code:

UIView *superview = self.view;

self.button.translatesAutoresizingMaskIntoConstraints = NO;
self.textField.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.textField
                                                              attribute:NSLayoutAttributeCenterY
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:superview
                                                              attribute:NSLayoutAttributeCenterY
                                                             multiplier:1.0f
                                                                constant:0];
[superview addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:self.textField
                                          attribute:NSLayoutAttributeCenterX
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:superview
                                          attribute:NSLayoutAttributeCenterX
                                         multiplier:1.0f
                                           constant:0];
[superview addConstraint:constraint];

constraint =[NSLayoutConstraint constraintWithItem:self.button
                                           attribute:NSLayoutAttributeTrailing
                                           relatedBy:NSLayoutRelationEqual
                                              toItem:self.textField
                                           attribute:NSLayoutAttributeLeading
                                          multiplier:1
                                            constant:-10];

[superview addConstraint:constraint];

constraint =[NSLayoutConstraint constraintWithItem:self.button
                                           attribute:NSLayoutAttributeBaseline
                                           relatedBy:NSLayoutRelationEqual
                                              toItem:self.textField
                                           attribute:NSLayoutAttributeBaseline
                                          multiplier:1
                                            constant:0];

[superview addConstraint:constraint];

And after running I have this issue: enter image description here Constraints also have no effect on views. What am I doing wrong?

Maria
  • 755
  • 1
  • 11
  • 29
  • Handosomeguy is correct. Is there any particular reason that makes you add more constraints programmatically? – Andrea Mar 18 '14 at 07:51
  • 1
    I don't want to add " more" constraints. I just want to create UIViews in IB and make all the constraints in code (for education purposes). So before creation of new constraints I need to remove all constraints from this UIViews, but it looks like they could not be removed if UIView was created in IB! – Maria May 09 '14 at 04:33
  • You can create in IB placeholder constraints, that doesn't exits at runtime, for each constraints created check in the panel inspector for a checkbox 'remove at runtime' (i,m on the ipad anche can't check now). Using those option the ib constraint will benremoved at runtime and you can start fromscratch. For the old problem is due to the fact that probably you are asking the removal to the wrong view or in the wrong 'moment' – Andrea May 09 '14 at 05:12

2 Answers2

2

First in the storyboard editor select the view controller you want to change then, in the constraint editor, select "ADD missing constraints in view controller" enter image description here Select an elment that you'd like to modify at runtime by remomiving and adding new constraint, in the object inspector panel select each constraints and edit it by checking the box "placeholder: remove at build time".

enter image description here

This is a way to say to storyboard editor that you are fine with the constraint ant it should not apply or complain about an insufficient constraints situation. Constraints flagged are removed at runtime, so you should provide you own constraint and is better if you do in the right place. Ovverride -updateViewConstraints in the view controller and remember to call super and add you new constraints.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

You have three auto-generated constraints "IB auto generated at build time for view with fixed frame". These conflict with the ones you make yourself. Probably, you have set fixed size properties on the text field and button in Interface Builder. Possibly, you have a fixed width on your button, and that conflicts with the trailing constraint.

Combining IB with code constraints can be tricky. I have found it is often easier to design things only in IB or only in code to avoid conflicts like these, although combining the two is perfectly viable.

For a good explanation on what NSIBPrototypingLayoutConstraint is, have a look at this SO answer: Trouble with AutoLayout on UITableViewCell

Community
  • 1
  • 1
Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • All I want is to make this textField and button in IB, but set all the constraints programmatically. I don't want to combine anything, than's why I set translatesAutoresizingMaskIntoConstraints for all views to NO – Maria Mar 18 '14 at 07:59
  • You sure there are no constraints in IB? – Marius Waldal Mar 18 '14 at 08:06
  • There are default costraints, I did not make my own constraints there. – Maria Mar 18 '14 at 09:58
  • Did you check the other answer I linked to? There are definitely a conflict between your own constraints and IB's constraints. Try the suggestions from that post. – Marius Waldal Mar 18 '14 at 10:20
  • The problem is I have no constraints to select in storyboard:( – Maria Mar 18 '14 at 11:12
  • I understand. In the post I linked, there is a suggestion to remove all constraints first, using the removeConstraintsAffectingView in the autolayout category found here: https://github.com/smileyborg/UIView-AutoLayout - I suggest you give that a try – Marius Waldal Mar 18 '14 at 12:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49945/discussion-between-handsomeguy-and-maria) – Marius Waldal Mar 18 '14 at 12:42