【问题标题】:UITextField hidden while keyboard is showing键盘显示时隐藏的 UITextField
【发布时间】:2015-02-04 09:59:57
【问题描述】:

在我的应用中,我有这个登录屏幕:

在第一个测试字段上方我有一张图片,Interface Builder 中的配置如下:

现在,当我点击 UITextField 时,它们应该向上移动,否则在 iPhone 4/4s 中,该字段将被键盘覆盖。 现在我正在使用以下代码:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35f];
    CGRect frameView = self.view.frame;
    CGRect frameImage = self.imageViewLogo.frame;
    CGRect frameTextFieldUsername = self.textFieldUsername.frame;
    CGRect frameTextFieldPassword = self.textFieldPassword.frame;
    CGRect frameButtonLogin = self.buttonLogin.frame;
    frameView.origin.y = -100;
    frameImage.origin.y = -100;
    frameTextFieldUsername.origin.y = -100;
    frameTextFieldPassword.origin.y = -100;
    frameButtonLogin.origin.y = -100;
    [self.view setFrame:frameView];
    [self.imageViewLogo setFrame:frameImage];
    [self.textFieldUsername setFrame:frameTextFieldUsername];
    [self.textFieldPassword setFrame:frameTextFieldPassword];
    [self.buttonLogin setFrame:frameButtonLogin];
    [UIView commitAnimations];
}

当我尝试在模拟器或真实设备上运行应用程序时,视图向上滚动 100,但图像、文本字段和按钮不向上滚动...我认为问题取决于约束,你能帮我解决这个问题吗? 谢谢

【问题讨论】:

  • 您必须添加 UIScrollView 并获取两个文本字段,然后下载 github.com/michaeltyson/TPKeyboardAvoiding。将 UIScrollView 的主类带到 TPKeyboardAvoidingScrollView。
  • 不需要使用scrollView等第三方类。检查我的答案。

标签: ios objective-c iphone


【解决方案1】:

您尝试这样做的方式会给您带来麻烦。当您给出帧的硬编码值时。

尝试使用keyboard avoiding library,这是更好、更安全的方法。

【讨论】:

    【解决方案2】:
    -(void)textFieldDidBeginEditing:(UITextField *)textField {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.35f];
        CGRect frameView = self.view.frame;
        CGRect frameImage = self.imageViewLogo.frame;
        CGRect frameTextFieldUsername = self.textFieldUsername.frame;
        CGRect frameTextFieldPassword = self.textFieldPassword.frame;
        CGRect frameButtonLogin = self.buttonLogin.frame;
        frameView.origin.y = -100;
        // no need of changing frames of TextFields inside the View, Just change the y-padding of View
        [self.view setFrame:frameView];
        [UIView commitAnimations];
    }
    

    将 y-padding 减少到 -120 并检查

    希望对你有帮助,谢谢

    【讨论】:

      【解决方案3】:

      如果您的视图设置为使用 AutoLayout,则更改框架将无济于事。

      您应该订阅UIKeyboardWillChangeFrameNotification 并在那里执行您的更改。如果有约束,您可以在此处更新约束常量并在父视图上调用 -setNeedsUpdateConstraints

      如果你想支持外接键盘,你必须弄清楚键盘是出现还是消失。

      否则你可以收听UIKeyboardWillShowNotificationUIKeyboardWillHideNotification

      【讨论】:

        【解决方案4】:

        在键盘自带时自动向上textFields不需要使用第三方类。

        创建一个静态tableView 并在每个静态单元格中设计每个textFields 和按钮。 TableView 自动处理自动向上 textFields 键盘来了。

        【讨论】:

          【解决方案5】:

          您好,简单易行的技巧是:-

          在您的班级中取出用户名或电子邮件 ID 文本字段的顶部约束,并且在文本字段上确实开始编辑委托减少约束的常量值,因此当您选择文本字段并且在文本字段结束编辑委托时它会上升再次设置约束的常量值到前一个,因此它将重置在前一个位置。

          eg:-假设您在 Storyboard 中设置 Top Constraint 值为 150

          -(void)textFieldDidBeginEditing:(UITextField *)textField {
             _topConstraint.constant = 10.00; 
          [UIView animateWithDuration:0.5f animations:^{
                              [self.view layoutIfNeeded];
                                      }];
          }
          -(void)textFieldDidEndEditing:(UITextField *)textField {
             _topConstraint.constant = 150.00; 
          [UIView animateWithDuration:0.5f animations:^{
                              [self.view layoutIfNeeded];
                                      }];
          }
          

          希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-26
            • 2015-01-17
            • 2022-01-23
            • 2011-05-03
            相关资源
            最近更新 更多