Mojo Jojo
Mojo Jojo

Reputation: 187

How to increase height of uiview when auto layout is enabled in objective c

I tried turning off auto layout for specific sub-views in interface builder at runtime programmatically using below code.

[view removeConstraints:view.constraints];

Somewhere someone suggested to do this:

If you need to change the height of a view with auto layout active, you'll need to create an IBOutlet for your height constraint and modify that at runtime, i.e.:

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
self.heightConstraint.constant = 200

Yet I don't understand what this means.

Upvotes: 0

Views: 2825

Answers (5)

Mojo Jojo
Mojo Jojo

Reputation: 187

After getting help from Gregory and Jan now i am able to explain it step by step.

1) Drag the height constraint of the uiView to your .h file.

2) Then refrence it with a name, in my case i use heightConstraint.

3) Use following code to do the trick.

_heightConstraint.constant = 650;
[_myview layoutIfNeeded];

Upvotes: 0

Tobi Nary
Tobi Nary

Reputation: 4596

Please read and understand Apple's documentation on auto layout.

For now, if you remove all constraints, that does not disable auto layout.

You need to reference the height constraint in code and modify it as suggested, then [view layoutIfNeeded] as suggested by other answers.

If there is no height constraint yet, you may also add a new one at runtime via addConstraints:.

Can you elaborate on what exactly it is you are missing to follow your cited suggestion?

Edit: Please also note that the suggestion you cited is in swift, not Obj-C, so copy & paste won't do the trick.

Edit 2:

Here we go, word by word.

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
  • @IBOutlet: The following variable declaration's value is to be injected from the interface builder. Hooked up via drag and drop.
  • weak: Keyword in both Obj-C and Swift. The reference count is not increased nor managed by the assignment or use of this variable. (cf. 'retain-cycle' and 'memory management'.)
  • var: This declaration's value may be reassigned after it's initialization. In contrast to let; in Swift, variables can be declared immutable.
  • heightConstraint: The name of the declared variable
  • :: read as 'of type'
  • NSLayoutConstraint!: Type of the variable declared. In this case an implicitly unwrapped optional of NSLayoutConstraint. This roughly translates to the Obj-C type NSLayoutConstraint*.

Now,

self.heightConstraint.constant = 200

sets the constant of the above declared variable somewhere in the code when is suits you. In this example to 200.

Upvotes: 1

yang
yang

Reputation: 31

@IBOutlet weak var heightConstraint: NSLayoutConstraint! 
self.heightConstraint.constant = 200

constant: The constant added to the multiplied second attribute participating in the constraint.

Unlike the other properties, the constant can be modified after constraint creation.

Setting the constant on an existing constraint performs much better than removing the constraint and adding a new one that's exactly like the old except that it has a different constant.

Upvotes: 0

darshan
darshan

Reputation: 1115

You need to add after setting constraint values.

[self layoutIfNeeded];

Upvotes: 0

Gregory Molette
Gregory Molette

Reputation: 155

You have to add :

[self layoutIfNeeded];

to your code after you changed your height constraint. it will refresh the view and the constraint. It simply means that instead of removing autolayout you can keep it to your view and create a constraint to change the height dynamically.

You can follow this tutorial to understand how autolayout works

Upvotes: 5

Related Questions