2

I have a UITextView at the bottom of screen.
It has leading, trailing bottom space constraints to the superview.
It has also height constraint with value (constant) 40.

I want to make this text view of flexible height as I go on typing more text in it. I can make flexible height by changing height constraint value to greater than or equal to as:

enter image description here

It works fine and text view height goes on increasing.

But when I make it, storyboard show this error:

enter image description here

I want to make constraint value greater than or equal to programmatically, not from storyboard.

I know that there is one property of NSLayoutConstraint called

public var relation: NSLayoutRelation { get }

but as we can see it is readonly.

How can I change this relation to one of:

public enum NSLayoutRelation : Int {   
    case LessThanOrEqual
    case Equal
    case GreaterThanOrEqual
}

Or is there any workaround make a view of flexible height in auto-layout based storyboard?


I know about making IBOutlet of constraint and changing it’s constant value, but in my situation I can not do this, it should be changed dynamically.

Cœur
  • 37,241
  • 25
  • 195
  • 267
D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
  • This blog post might help you to understand how it works: http://devetc.org/code/2014/07/07/auto-layout-and-views-that-wrap.html – DevAndArtist Mar 28 '16 at 19:12

2 Answers2

1

Related to: auto layout modify multiplier of constriaint programmatically

With ARC:

- (void)changeConstraintRelationToEqual:(NSLayoutConstraint * __strong *)constraint{
    NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:(*constraint).firstItem attribute:(*constraint).firstAttribute relatedBy:NSLayoutRelationEqual toItem:(*constraint).secondItem attribute:(*constraint).secondAttribute multiplier:(*constraint).multiplier constant:(*constraint).constant];
    [self.view removeConstraint:(*constraint)];
    [self.view addConstraint:newConstraint];
    *constraint = newConstraint;
}
Community
  • 1
  • 1
TheJeff
  • 3,665
  • 34
  • 52
0

I've solved this in the past by marking the constraint I need to set at run time as "Placeholder - remove at build time" in Interface builder and then supplying a constraint based on the dynamic value programmatically by adding the constraint via code.

Owen Hartnett
  • 5,925
  • 2
  • 19
  • 35