ios - 按下 TextField 时向上移动 UIView

标签 ios xcode uitextfielddelegate

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

enter image description here

最佳答案

您必须使用 scrollView并将其他 UI 元素作为 ScrollView 的 subview .和框架尺寸为scrollView即使在 Landscape Mode 中也应该使用整个窗口大小也(为此你需要学习 autoresizingmask ).. 然后当你调用 textFieldkeyboard 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/

相关文章:

ios - 在选项卡栏 Controller 中将数据从一个 View 传递到另一个 View 未按时传递

ios - 从不同的 View 触发 segue

iphone - 所有区域格式列表

objective-c - 如何在 Xcode 6 beta3 中按顺序运行测试

ios - 同一个方案,为什么faSTLane xcov的代码覆盖率值和Xcode的值不一样?

ios - Flash Builder AIR 模拟器上的触摸手势

iOS - 显示来自 tableView didSelect 的信息

ios - 用于密码字段清除的文本字段 shouldChangeCharactersIn

ios - 填写所有 UITextField 后,UIButton 未启用

ios5 - 调用 UITextField 委托(delegate)程序时 iPhone 模拟器崩溃(带有 EXC_BAD_ACCESS)