1

我有 2 个文本字段,如果单击文本字段(在纵向模式下),键盘不会隐藏文本字段。但是当我更改为 Landscape 时,键盘会覆盖 textFields。我不知道如何在委托方法(textFieldShouldBeginEditing,textFieldShouldEndEditing)中设置帧大小,以便它可以在纵向和横向上工作。或者我应该使用滚动视图吗?在此处输入图像描述

在此处输入图像描述

4

3 回答 3

3

您必须使用 ascrollView并将其他 UI 元素作为ScrollView. 并且框架大小scrollView也应该使用整个窗口大小Landscape Mode(为此您需要学习autoresizingmask)..然后当您调用textFieldand时,即使您的键盘在视图中keyboard appears,您也需要减小框架的大小以滚动..scrollView

下面是我的 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];
}
于 2012-05-10T15:45:17.540 回答
1

我无法给你一个 100% 确定的答案,既因为它有点主观,也因为我无法真正看到你的应用程序(打印屏幕会很好)。但是,在我看来,aUIScrollView将是一个很好的答案。另一种选择是为您的 动画制作动画UITextField,以便在键盘出现时它可以“向上”。

于 2012-05-10T15:02:58.720 回答
1

(void)textFieldDidBeginEditing:(UITextField *)textField您应该将视图控制器的框架设置为在方法的屏幕中心显示编辑框(例如) 。计算应取决于方向。并将框架设置为原始的(void)textFieldDidEndEditing:(UITextField *)textField.

还有不同的方法,例如使用滚动视图并在触摸编辑框时更改其内容偏移量。

于 2012-05-10T15:06:50.467 回答