0
votes

I have a UITextField inside of a UITableView cell. When I rotate the device, the textfield is still the same width it was for Portrait mode. I know that I need to use autoresizing to make the field fill the cell when I am in landscape, but I am not sure how to do this, as I have tried and failed.

How would I do this?

cell.textLabel.text = @"Email";
UITextField *field2 = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, width, 30)];
field2.adjustsFontSizeToFitWidth = YES;
field2.textColor = [UIColor blackColor];
field2.keyboardType = UIKeyboardTypeEmailAddress;
field2.returnKeyType = UIReturnKeyNext;
field2.placeholder = @"Required";
field2.tag = 3;
field2.delegate = self;
field2.autoresizingMask =  UIViewAutoresizingFlexibleWidth;
field2.backgroundColor = [UIColor blueColor];
// Finish building cell
field2.autocorrectionType = UITextAutocorrectionTypeNo;
field2.autocapitalizationType = UITextAutocapitalizationTypeNone;
field2.textAlignment = UITextAlignmentLeft;
[field2 addTarget:self action:@selector(updateFields:) forControlEvents:UIControlEventEditingChanged];
field2.text = email;
self.emailField = field2;
[cell addSubview:field2];
[field2 release];
1
I find it useful to set the background color to pink (or an equally hideous color) when testing/debugging layout. I'd just do that and experiment with the values and see how they effect it. You'll likely want to bitwise OR several values together. - - - If it were me, id be building it in Xcode/InterfaceBuilder though. Writing interface layout/design in code is undesirable (and i've avoided it since X-Windows in the early 90s.) - bshirley
I did set my bg color to blue for testing. - Nic Hubbard

1 Answers

2
votes
  1. Check if you tableView has Auto Resize subivew is set to YES.
  2. Check you are returning YES for all the supported rotations.
    • (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration{ }