0

I have created the IBOutlet for the constraint of top spacing . I need to update the value that constraint programatically in viewDidLoad. Here is my declaration in IBOutlet :

 IBOutlet NSLayoutConstraint *labelTopSpace;

and here is how I changing the top:

labelTopSpace.constant = 50.0;

but this is not working in my case . am I missing anything ?

Update : should I make the property of it ?

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • do drag and drop for outlet........make a proprty – Bhavin Bhadani Apr 15 '15 at 12:33
  • try calling [self.view layoutIfNeeded]; or [self.view updateConstraints]; after assigning constant value.! Also check whether you have connected constraint at IB or not.. – Vidhyanand Apr 15 '15 at 12:48
  • in `–viewDidLoad` you might not be able to update anything of UI efficiently, as then the view is not part of view hierarchy yet. you would be able to do it later, when it becomes part of a navigation stack or view hierarchy. – holex Apr 15 '15 at 13:29

3 Answers3

0

Make it property, like

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

then check outlet connection. similarly asked question Unable to make outlet connection to a constraint in IB

Community
  • 1
  • 1
bhavesh
  • 109
  • 14
0

Hope, you have mentioned all the constraints under method!

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    _labelTopSpace.constant = 50;
}
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

Try using below code..

[btn setConstraintConstant:50.0 forAttribute:NSLayoutAttributeTop];

you can replace the btn with any Ref which for which you have that Constraint that need to modified.{say self.view//if it is for view}

In this case you no need to create property or IBOulet reference to your Constraint..

You can Directly change as above..

Hope it is useful to you as alternate solution...!

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59