Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar requirement with a <code>UITextView</code> (had to dynamically increase it's height and some other things).</p> <p>What I did was something similar to this:</p> <p>Considering <code>self.contentView</code> is the <code>superview</code> of <code>textField</code></p> <pre><code>- (IBAction)textFieldDidChange:(UITextField *)textField { //You can also use "textField.superview" instead of "self.contentView" [self.contentView setNeedsUpdateConstraints]; //Since you wish to animate that expansion right away... [UIView animateWithDuration:0.1 animations:^{ [self.contentView updateConstraintsIfNeeded]; }]; NSLog(@"%@", NSStringFromCGSize(textField.intrinsicContentSize)); } </code></pre> <p>Also, considering the requirements I had, I had to override the <code>updateConstraints</code> method of my <code>UITextView</code>'s <code>superview</code>.</p> <p>Should you choose to opt for that solution (maybe for a more fine tuned approach), you can do:</p> <pre><code>- (void)updateConstraints { [super updateConstraints]; //Firstly, remove the width constraint from the textField. [self.myTextField removeConstraint:self.textFieldWidthConstraint]; self.textFieldWidthConstraint = nil; CGSize contentSize = self.myTextField.intrinsicContentSize; self.textFieldWidthConstraint = [NSLayoutConstraint constraintWithItem:self.myTextField attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:contentSize.width]; [self.myTextField addConstraint:self.textFieldWidthConstraint]; } </code></pre> <p>As mentioned, the override option was used by me because I required a more fine tuned approach.</p> <p>Additionally, as always you'll need to make sure that you're checking for any edge cases (in terms of sizing values) that you think you might encounter.</p> <p>Hope this helps!</p> <p>Cheers!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload