【问题标题】:Swift - Get keyboard input as the user is typingSwift - 在用户输入时获取键盘输入
【发布时间】:2015-07-06 09:17:42
【问题描述】:

我有一个 swift 应用程序,texfield 位于屏幕底部,当用户尝试输入任何内容时,键盘会覆盖文本字段,因此用户无法看到他/她正在输入的内容.为了解决这个问题,我想从用户那里获取键盘输入,每次敲击/“类型”(每次用户按下一个键),然后将其显示到位于键盘正上方的视图上。如果这不可能或非常复杂,我们非常欢迎您提出替代解决方案。

【问题讨论】:

    标签: ios swift keyboard textfield keyboard-input


    【解决方案1】:

    您需要注册为通知观察者才能查看键盘何时出现和消失。然后,您可以在显示时将视图向上移动,或在隐藏时将其恢复为原始视图。

    我的实现将整个视图向上移动键盘高度,但如果您愿意,您可以向上移动 UITextField。

    你可以看看这个教程,或者在答案中查看我的实现:

    http://www.ioscreator.com/tutorials/move-view-behind-keyboard-ios8-swift

    在viewDidAppear中添加观察者

    override func viewWillAppear(animated:Bool) {
        super.viewWillAppear(animated)
    
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    

    然后您需要在 keyboardWillShow 和 keyboardWillHide 新方法中移动视图的方法。

    @objc func keyboardWillShow(_ notification: NSNotification) {
        let info = notification.userInfo!
        let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    
        let keyboardHeight:CGFloat = keyboardSize.height
    
        //let animationDuration:CGFloat = info[UIResponder.keyboardAnimationDurationUserInfoKey] as! CGFloat
    
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
            self.view.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight)
            }, completion: nil)
    
    }
    

    并将视图移动到原始状态:

    @objc func keyboardWillHide(_ notification: NSNotification) {
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
            self.view.transform = CGAffineTransform.identity
            }, completion: nil)
    }
    

    最后删除viewDidDisappear中的观察者

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }
    

    【讨论】:

    • 我刚刚在我的代码中找到了一个实现,所以我添加了它。它应该可以工作,如果您有任何问题,请询问:)
    • 你有什么问题吗?
    • 抱歉耽搁了,但这正是我想要的!非常感谢你,你让我的生活变得轻松多了:)
    • 如果可以的话我会投票,但我没有足够的声誉
    【解决方案2】:

    为了配合 Stefan Salatics 的回答,我使用以下代码,因为键盘通常仍会覆盖文本字段。

    -(void) keyboardWillShow:(NSNotification *)note
    {
    // Get the keyboard size
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
    
    // Start animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    
    
    // Get Keyboard height and subtract the screen height by the origin of the textbox and height of text box to position textbox right above keyboard
    self.scrollView.contentOffset = CGPointMake(0,keyboardBounds.size.height-([UIScreen mainScreen].bounds.size.height - commentBox.frame.origin.y - commentBox.frame.size.height));
    
    [UIView commitAnimations];
    }
    

    【讨论】:

      【解决方案3】:

      尽管 Stefan 的回答更适合您当前的实现。您的问题是如何获取用户输入的每一个笔划。

      为了完成

      class viewController: UIViewController, UITextFieldDelegate{    
          var textField = UITextField()
      
          override func viewDidLoad() {
              textField.delegate = self
          }
      
          func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
              //string is used here as the value of the textField.
              //This will be called after every user input in the textField
          }
      }
      

      string 可以用来查看最新的用户输入是什么。您可以使用当前值存储一个局部变量,并将其与“新”值进行比较,以查看发生了什么变化。

      【讨论】:

        【解决方案4】:

        以下是我解决该问题的方法。我希望这个方法能解决你的问题。

        这是您要使用的文本字段。

        @IBOutlet var userID : UITextField!
        

        你需要编写这个函数。

        func textFieldShouldReturn(textField: UITextField!)-> Bool
        {   userID.resignFirstResponder( )
            return true
        }
        

        在你想要的函数中,你需要编写这个语法。

        @IBAction func login(sender: AnyObject)
        {
            userID.resignFirstResponder( )
        }
        

        这是在 ViewDidLoad 或 ViewDidAppear 中

        override func viewDidLoad( )
        {
            userID.delegate = self;
        }
        

        我希望这对你有用。

        【讨论】:

          猜你喜欢
          • 2013-07-06
          • 1970-01-01
          • 2022-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多