0

In my xib file, I have a constraint for height for the label in my xib file. And in my objective c, I tried to change it using

self.heightConstraint.constant = newHeight;

But nothing is changed when I run it in simulator. I have used debugger and make sure that line is executed. And in Spark tool, I see the label height is the old height.

How can I adjust the height dynamically?

Updated:

I have changed my code to add a nil check.

if (self.heightConstraint != nil) {
self.heightConstraint.constant = newHeight;
}

My code still get executed, but nothing get changed.

n179911
  • 19,547
  • 46
  • 120
  • 162
  • possible duplicate of [Creating layout constraints programmatically](http://stackoverflow.com/questions/12826878/creating-layout-constraints-programmatically) – rebello95 Jan 26 '15 at 19:19
  • Have you tried adding `[self setNeedsLayout]; [self layoutIfNeeded];`? – Ian MacDonald Jan 26 '15 at 19:29
  • I have added[self setNeedsLayout]; [self layoutIfNeeded]; after I update the heightConstraint. But what does not help either. – n179911 Jan 26 '15 at 20:09
  • Your updated code does not help: if self.heightConstraint == nil, your update code does nothing, exactly the same as without your update code. Add an else {assert(NO);} and see, if it stops there! – Reinhard Männer Jan 26 '15 at 20:34

3 Answers3

0

There's nothing wrong with your code itself:

self.heightConstraint.constant = newHeight;

But the reason this changes nothing in your interface, even though it is being executed, is that self.heightConstraint is not a reference to a constraint in your interface. (It is probably nil.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

I agree with Matt. The likely reason your code isn't working is that you have a broken outlet link. In general, when code that tries to do something with an outlet or action doesn't work, the most likely cause is a broken outlet or action connection.

Rule of thumb: When debugging stereo or computer hardware, check the cables.

Corollary for for iOS or Mac OS apps in Xcode: When debugging UI code, check your IBOutlet and IBAction connections.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

At what part of the view controller lifecycle are you changing the constraints? You may need to call setNeedsLayout on the parent view.

psobko
  • 1,548
  • 1
  • 14
  • 24