我有一个非常简单的 UITableView,它在左侧包含一个固定标签,然后在每个单元格中包含一个 UITextField。当设备旋转时,我需要 uitextfield 与单元格一起扩展和收缩其宽度。我试过设置
自动调整子视图 = 是;autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
对于 tableview、cell 和 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];
任何帮助将非常感激!!谢谢!!