[ios] Autoresize View When SubViews are Added

I am developing an app using iOS 7 and auto layout. I am adding a simple textfield to a UIView. But the view is not getting resized. I even tried to change the frame manually but it did not work. I believe it is because I am using auto layout. Here is my code:

-(void) setupViewForSignUp {     UITextField *firstNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.passwordTextField.frame.origin.x, self.passwordTextField.frame.origin.y + 40, self.passwordTextField.bounds.size.width, self.passwordTextField.bounds.size.height)];     firstNameTextField.placeholder = @"First Name";     firstNameTextField.layer.borderWidth = 0.3;     firstNameTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];     firstNameTextField.leftViewMode = UITextFieldViewModeAlways;      [self.signUpLoginContainerView addSubview:firstNameTextField];      self.signUpLoginContainerView.autoresizingMask = UIViewAutoresizingFlexibleHeight;  } 

Here is the result:

enter image description here

This question is related to ios autolayout

The answer is


Yes, it is because you are using auto layout. Setting the view frame and resizing mask will not work.

You should read Working with Auto Layout Programmatically and Visual Format Language.

You will need to get the current constraints, add the text field, adjust the contraints for the text field, then add the correct constraints on the text field.