Sunday, 8 September 2013

Resizing UITextView Without Losing Constraints

Resizing UITextView Without Losing Constraints

I'm trying to figure out how to resize UITextView (and everything
actually) without losing constraints. Basically, I'm trying to layout a
page where most components can have variable sizes (like description). I
tried doing it with a simple use case where I have a UITextView and a
UIButton underneath. I want to make sure that the position of the button
is relative to the bottom of the UITextView.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect frame = self.textView.frame;
int height = self.textView.contentSize.height;
frame.size.height = height;
self.textView.frame = frame;
}
What I ended up with is UITextView overlapping with UIButton. After doing
a bit of research, it seems that if I replace the frame, all constraints
are gone also. I tried copying the constraints over, but of course the
pointer is still pointing at the old frame so that didn't help at all.
Is there a good way to solve a very dynamically laid out page? I'm trying
to at least use interface builder rather than code everything.
EDITED
I tried updating the constraint as suggest, but that didn't actually
resize the UITextView. Did I do it incorrectly? When I get the constant
again, it's updated, but the height isn't changed visually.
int height = self.textView.contentSize.height;
NSArray *constraints = [self.textView constraints];
for(int i = 0; i < [constraints count]; i++){
NSLayoutConstraint *constraint = [constraints objectAtIndex:i];
NSLayoutAttribute attribute = [constraint firstAttribute];
CGFloat ori = [constraint constant];
if(attribute == NSLayoutAttributeHeight){
[constraint setConstant: (CGFloat)height];
}
CGFloat newHeight = [constraint constant];
}

No comments:

Post a Comment