【问题标题】:iPhone - Keyboard hides TextFieldiPhone - 键盘隐藏文本字段
【发布时间】:2011-01-19 09:58:48
【问题描述】:

我正在使用 UITextField 接收用户输入。但是,由于我的文本字段朝向笔尖的中间/底部,因此当键盘弹出时它会被隐藏。有什么方法可以将它与键盘一起滑动,以便在选择过程中它位于键盘的顶部?另外,由于我也在使用数字键盘,有没有一种简单的方法可以在某处包含完成按钮?

感谢您的帮助。

【问题讨论】:

标签: iphone keyboard uitextfield


【解决方案1】:

我为这个问题做了一个简单的应用程序。它会自动检查 Textfield 的位置,如果键盘隐藏它,它会根据需要自动向上移动。

- (void)textFieldDidBeginEditing:(UITextField *)textField { [self animateTextField:textField up:YES]; } - (void)textFieldDidEndEditing:(UITextField *)textField { [self animateTextField:textField up:NO]; } - (void) animateTextField: (UITextField*) textField up: (BOOL) up { int animatedDistance; int moveUpValue = textField.frame.origin.y+ textField.frame.size.height; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { animatedDistance = 216-(460-moveUpValue-5); } else { animatedDistance = 162-(320-moveUpValue-5); } if(animatedDistance>0) { const int movementDistance = animatedDistance; const float movementDuration = 0.3f; int movement = (up ? -movementDistance : movementDistance); [UIView beginAnimations: nil context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.view.frame, 0, movement); [UIView commitAnimations]; } } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }

【讨论】:

  • 我在我的第 8 个文本字段中,我点击了标签栏,就像一个神奇的整体视图消失了。为什么?
  • @Dee 请给我代码.. 我可以查看并回复您。
  • 如果您使用此代码,请使用 [UIView transitionWithView...] 而不是 [UIView beginAnimations...],[UIView setAnimationBegins...],[UIView setAnimationDuration...],[UIView commitAnimations].
  • 这很好用——我真的很想避免使用 ScrollView,这成功了。因为我是一个 n00b,所以我花了一点时间才意识到我必须设置每个 textField.delegate = self 才能触发“textFieldDidBeginEditing”。
  • 这对我来说确实很有效(现在使用 Swift),但是当我触摸一个 textField 并且键盘出现时,我会在“离开”那个 textField 之前导航到另一个屏幕......如果我回到第一个屏幕,键盘还在。然后我触发场地,它会移动负距离并在页面上方显示黑色空间。
【解决方案2】:

要在键盘出现时滚动,我喜欢 Cocoa With Love 的 this tutorial

要关闭数字键盘,您可以put a custom "Done" button on the keypad 或在屏幕的其余部分制作一个不可见的按钮。我已经用代码完成了后者,但是this tutorial 使用了Interface Builder。

【讨论】:

  • 第三个链接坏了:(
  • 基于这个答案中的第一个教程链接,我想出了这个要点:gist.github.com/siannopollo/7892526 它执行的滚动越来越少,越来越一致。
  • 这是一个价值百万美元的答案。但是,第二个和第三个链接不起作用。
【解决方案3】:

您必须手动执行此操作。看看这个答案。 UITextField: move view when keyboard appears

【讨论】:

    【解决方案4】:

    下面的代码非常适合我。

    [textFieldFocused becomeFirstResponder];
    [self.view addSubview:startView];
    [textFieldFocused resignFirstResponder];
    

    【讨论】:

    • 你能在这里描述一下startView是什么吗?
    • startView 只是另一个视图,它可以是任何视图。关键是 becomeFirstResponder 然后辞职。
    【解决方案5】:

    使用此代码:

    - (void)viewDidAppear:(BOOL)animated
    {
    [super viewDidAppear:animated];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidShow:) 
                                                 name:UIKeyboardDidShowNotification 
                                               object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardDidHide:) 
                                                 name:UIKeyboardDidHideNotification 
                                               object:self.view.window];
    }
    
    
    - (void)viewDidDisappear:(BOOL)animated {
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardDidShowNotification 
                                                  object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIKeyboardDidHideNotification 
                                                  object:nil];
    }
    
    - (void)keyboardDidHide:(NSNotification *)n
    {
    CGRect viewFrame = scrollView.frame;
    UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
    if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft)) 
        viewFrame.size.height += 140;
    else viewFrame.size.height += 216; //portrait mode
    scrollView.frame = viewFrame;
    keyboardVisible = NO;
    }
    
    - (void)keyboardDidShow:(NSNotification *)n
    {
    CGRect viewFrame = scrollView.frame;
    UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
    if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft)) 
        viewFrame.size.height -= 140;
    else viewFrame.size.height -= 216; //portrait mode
    scrollView.frame = viewFrame;
    keyboardVisible = YES; }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    [textField resignFirstResponder];
    return YES;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    CGRect viewFrame = scrollView.frame;
    if ((interfaceOrientation == UIDeviceOrientationLandscapeRight) || (interfaceOrientation == UIDeviceOrientationLandscapeLeft)) //wants to change to landscape mode
        if (keyboardVisible == NO)//currently in portrait,check if keyboard was present
                viewFrame.size = CGSizeMake(480,250);
        else viewFrame.size = CGSizeMake(480,170);
    else {//wants to change to portrait mode
        if (keyboardVisible == NO)//currently in landscape,check if keyboard was present
            viewFrame.size = CGSizeMake(320,416);
        else viewFrame.size = CGSizeMake(320,200);
    }
    scrollView.frame = viewFrame;
    
    return YES;
    }
    

    也适用于横向模式,但仅适用于 iphone。对于 iPad,相应地更改框架设置。 textFieldShouldReturn: 方法将在按下返回时隐藏键盘。希望这会有所帮助...

    【讨论】:

    • 什么是scrollView参数?
    • 这是包含文本字段的滚动视图的名称。
    • 这可能会导致问题,因为 viewDidUnload 在 iOS 6 及以后的版本中永远不会被调用,因此您实际上并没有取消注册监听器。
    • @ljboy 好点。我已经更新了答案,使其适用于iOSes。但是公认的和其他最佳答案是要走的路。那时我只是在尝试一下:D
    【解决方案6】:

    如果有人需要,我已经将 ValayPatel 的解决方案翻译成 swift

    func animatedTextField(textField: UITextField, up: Bool){
        var animatedDistance : Int = 0
        let moveUpValue : Int = Int(textField.frame.origin.y) + Int(textField.frame.size.height)
    
        switch UIDevice.currentDevice().orientation {
        case .Portrait , .PortraitUpsideDown:
            animatedDistance = 216 - (460 - moveUpValue - 5)
        default:
            animatedDistance = 162 - (320 - moveUpValue - 5)
        }
    
        if (animatedDistance > 0 ){
    
            let movementDistance : Int = animatedDistance
            let movementDuration : Float = 0.3
            let movement = up ? -movementDistance : movementDistance
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationBeginsFromCurrentState(true)
            UIView.setAnimationDuration(NSTimeInterval(movementDuration))
            self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
            UIView.commitAnimations()
    
        }
    
    }
    

    【讨论】:

    • 请将前一行改为self.view.frame = self.view.frame.offsetBy(dx: 0, dy: CGFloat(movement))
    【解决方案7】:

    这种情况对 iPhone 来说有点糟糕。您应该做的是设计您的界面,使键盘出现时字段可见,或者在键盘出现时向上移动字段。 (完成后退后)

    我建议查看一些 Apple 应用,例如“通讯录”或“设置”,看看他们是如何处理这个问题的。

    另外,我相信 iPhone HIG 有话要说。

    【讨论】:

      【解决方案8】:

      我知道我回答这个问题有点晚了,但是我找到了一个非常简单的解决方案。如果您使用 UITableView 控制器而不是 UIViewController 将 tableView 作为对象,则不会出现此问题。

      当您单击表格底部的文本字段时,键盘会弹出,表格视图会自动向上滚动,直到正在编辑的文本字段可见。

      但是,当我们使用键盘上的返回按钮时,自动滚动不起作用。在这种情况下,我们手动向上滚动表格以查看文本字段。

      希望对你有帮助

      【讨论】:

        【解决方案9】:

        我一直在寻找类似问题的无数答案。对于基本的单屏应用程序,这个解决方案就像一个魅力,实现起来非常简单:https://github.com/michaeltyson/TPKeyboardAvoiding

        当然还有更优雅的解决方案,但这样可以快速完成工作。

        【讨论】:

          【解决方案10】:

          设置 UITableView 为编辑模式就这么简单!

          - (void)viewDidLoad {
          
              [super viewDidLoad];
          
              [self.tableView setEditing:YES];
          }
          

          如果你想隐藏一个删除气泡,在一个单元格的左边,然后实现一个 UITableViewDelegate 方法:

          - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
          
              return NO;
          }
          

          【讨论】:

            【解决方案11】:
            - (void)textFieldDidBeginEditing:(UITextField *)textField
            {
                NSLog(@"%f",textField.frame.origin.y);
            
                CGPoint scrollPoint = CGPointMake(0, textField.frame.origin);
                [scrollView setContentOffset:scrollPoint animated:YES];
            }
            
            - (void)textFieldDidEndEditing:(UITextField *)textField 
            {
                [scrollView setContentOffset:CGPointZero animated:YES];
            }
            

            【讨论】:

              【解决方案12】:
              - (void)textFieldDidBeginEditing:(UITextField *)textField
              {
                  [self animateTextField:textField up:YES];
              }
              

              请在您的视图控制器.m 文件中使用此代码..当您单击文本字段时使用此代码,键盘将出现。再次,当您单击视图时,键盘将被隐藏..希望这对您有所帮助。 ..

              【讨论】:

                猜你喜欢
                • 2014-03-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-12-22
                • 2021-05-07
                • 2021-05-20
                • 2023-03-10
                • 1970-01-01
                相关资源
                最近更新 更多