我有一个非常简单的UITableView,它在左侧包含一个固定标签,然后在每个单元格中包含一个UITextField。当设备旋转时,我需要uitextfield来扩展和收缩它与单元格的宽度。我试过设置
autoresizesSubviews = YES; autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
对于tableview,单元格和cell.contentView,它们似乎都没有影响textField。
当我将实际textField的autoresizingMask设置为flexibleWidth和FlexibleRightMargin时,无论它在什么方向,textField都会远离屏幕。
这是我创建textField并将其添加到单元格的代码:
int marginBetweenCellAndScreenEdge;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
marginBetweenCellAndScreenEdge = 44;
} else {
marginBetweenCellAndScreenEdge = 10;
}
float viewWidth = self.parentViewController.view.bounds.size.width;
int textFieldWidth = viewWidth - (2*marginBetweenCellAndScreenEdge) - 75 - 20;
NSLog(@"%d", textFieldWidth);
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, textFieldWidth, 25)];
textField.clearsOnBeginEditing = NO;
[textField setDelegate:self];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = kTextFieldTag;
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//make it so that the UITextField subview resigns the firstresponder (keyboard) when done editing
[textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:textField];
任何帮助将不胜感激!!谢谢!