Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto Layout with UITextField causes exception
    primarykey
    data
    text
    <p>I'd like to position a <strong>UITextField</strong> inside a <strong>UIViewController</strong> view using auto layout (dynamic resizing and a 5 pt margin between the textfield and its superview). I create the view programmatically using the <strong>loadView</strong> method.</p> <p>Header of UIViewController</p> <p><code>@property (weak, nonatomic) UITextField *textField;</code></p> <p>Implementation</p> <pre><code>-(void)loadView{ //Frame UIView *frame = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)]; [self setView:frame]; [self.view setBackgroundColor:[UIColor redColor]]; //TextField [self.textField setDelegate:self]; [self.textField setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.textField setBackgroundColor:[UIColor blueColor]]; [frame addSubview:self.textField]; [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(5@50)-[textField(==10@10)]-(5@50)-|" options:0 metrics:nil views:@{@"textField" : self.textField}]]; [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-(5@50)-[textField(==10@10)]-(5@50)-|" options:0 metrics:nil views:@{@"textField" : self.textField}]]; // //Test with UIView instead // UIView *sub = [[UIView alloc]init]; // [sub setTranslatesAutoresizingMaskIntoConstraints:NO]; // [sub setBackgroundColor:[UIColor greenColor]]; // [frame addSubview:sub]; // // [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(5@50)-[sub(==10@10)]-(5@50)-|" // options:0 // metrics:nil // views:@{@"sub" : sub}]]; // // [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-(5@50)-[sub(==10@10)]-(5@50)-|" // options:0 // metrics:nil // views:@{@"sub" : sub}]]; </code></pre> <p>Running this throws an exception. Curiously using a UIView ("sub") as subview instead (the part commented out) does work as expected.</p> <p>Ouput:</p> <p><img src="https://i.stack.imgur.com/LiD8M.png" alt="Auto Layout with UIView"></p> <p>Shouldn't the textfield behave just like this UIView? How come the textField causes an exception?</p> <hr> <p>New finding: The app crashes only when I use <strong>addConstraints</strong> on the <strong>self.textField</strong>. If I remove those two calls I spare the crash, however the textfield doesn't show up either.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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