我有 2 个文本字段,如果单击文本字段(在纵向模式下),键盘不会隐藏文本字段。但是当我更改为 Landscape 时,键盘会覆盖 textFields。我不知道如何在委托(delegate)方法(textFieldShouldBeginEditing,textFieldShouldEndEditing)中设置帧大小,以便它可以在纵向和横向上工作。或者我应该使用 ScrollView 吗?
最佳答案
您必须使用 scrollView
并将其他 UI 元素作为 ScrollView
的 subview .和框架尺寸为scrollView
即使在 Landscape Mode
中也应该使用整个窗口大小也(为此你需要学习 autoresizingmask
).. 然后当你调用 textField
和 keyboard appears
,您需要减小我们的 scrollView
的大小即使您的键盘在 View 中,也可以滚动框架..
下面是我的 textFieldDidBeginEditing 和 textFieldDidEndEditing 方法的代码:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.2];
scrollView.frame = CGRectMake(0,0,320,150);
[scrollView scrollRectToVisible:textField.frame animated:YES];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self autoCalculateDownPayment];
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.2];
scrollView.frame = CGRectMake(0,0,320,416);
[UIView commitAnimations];
}
关于ios - 按下 TextField 时向上移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10536760/