【问题标题】:Swift: Additional help on the existing TextView move up + Keyboard behaviorSwift:对现有 TextView 上移 + 键盘行为的附加帮助
【发布时间】:2016-07-02 15:13:52
【问题描述】:

我从this existing question 学到了让函数“keyboardwillshow”将整个视图向上移动相同的键盘高度,以避免底部的文本字段/文本视图被键盘覆盖。

但是,如果我连续点击两个或更多文本字段,“keyboardwillshow”函数将运行相同的点击次数,进一步向上移动视图并最终离开屏幕,只留下黑色空白。

override func viewDidLoad() {
    super.viewDidLoad()


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

}


func keyboardWillShow(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        self.view.frame.origin.y -= keyboardSize.height
    }

}

func keyboardWillHide(notification: NSNotification) {
    self.view.frame.origin.y = 0
}

有人在回答中说,我们可以实现一个布尔值来检测键盘是否已经出现,所以这个函数只会工作一次。

有人可以告诉我如何做这个布尔值吗?谢谢!

【问题讨论】:

    标签: swift keyboard


    【解决方案1】:

    首先添加一个实例变量来存储布尔值并初始化为 false (var keyboardIsShown = false。接下来在 keyboardWillShow 中的 if 语句中,将您之前创建的 boolean 设置为 true,因为键盘现在已经如图所示。下一步是在移动视图之前在keyboardWillShow 中添加另一个if 语句,以检查键盘是否已经显示。最后确保在键盘再次隐藏时将boolean 设置回false .

    // instance variable here
    
      override func viewDidLoad() {
            super.viewDidLoad()
    
    
            NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
    
        }
    
    
        func keyboardWillShow(notification: NSNotification) {
    
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                // if statement here
                    self.view.frame.origin.y -= keyboardSize.height
                    // set boolean to true
            }
    
        }
    
        func keyboardWillHide(notification: NSNotification) {
            self.view.frame.origin.y = 0
            // set boolean to false
        }
    

    【讨论】:

      【解决方案2】:

      好的。我发现更好的解决方案是使用 UITextFieldTextDidBeginEditingNotification 和 UITextViewTextDidBeginEditingNotification,或者 UITextViewTextDidBeginEditingNotification 和 UITextViewTextDidEndEditingNotification。

      例子:

          NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UITextFieldTextDidBeginEditingNotification, object: yourTextField)
      
          NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UITextFieldTextDidEndEditingNotification, object: yourTextView)
      

      这样,键盘行为仅适用于标识为 object: 的指定文本字段或文本视图。如果您希望它适用于视图中的所有文本字段或文本视图,则 object 为 nil。

      【讨论】:

        猜你喜欢
        • 2018-07-19
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 2017-12-18
        • 1970-01-01
        • 2012-04-15
        • 1970-01-01
        • 2011-02-08
        相关资源
        最近更新 更多