【问题标题】:Move up textfield above the keyboard向上移动键盘上方的文本字段
【发布时间】:2016-07-07 17:48:29
【问题描述】:

我尝试使用某些东西来向上移动视图,因为当我进入表单末尾并点击最后一个文本字段时,我看不到我写的内容,因为键盘出现在文本字段上方,所以我发现了一些东西,但是在这里如果我点击视图顶部的文本字段,这将通过向上隐藏,这就是代码:

override func 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) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        self.view.frame.origin.y += keyboardSize.height
    }
}

但是这里还有一个问题,当我在另一个文本字段上点击两次时,会出现一个黑色块。

所以我不想做的是检测文本字段并将键盘放在它下面,以便向上移动文本字段。并检测键盘是否已经在这里不要显示黑色块。但是我还没有找到解决办法。

【问题讨论】:

标签: ios swift keyboard uitextfield


【解决方案1】:

我在我的项目中使用了这个代码:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    // Register Keyboard Notification
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    // Remove Keyboard notification observer
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification)
{
    var userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)

    var contentInset:UIEdgeInsets = self.scrollView.contentInset
    contentInset.bottom = keyboardFrame.size.height - 30 //Set this value (30) according to your code as i have navigation tool bar for next and prev.
    self.scrollView.contentInset = contentInset
}

func keyboardWillHide(notification: NSNotification) {
    let contentInset:UIEdgeInsets = UIEdgeInsetsZero
    self.scrollView.contentInset = contentInset
}

希望对你有帮助!!

【讨论】:

    【解决方案2】:

    添加一个 bool 属性 self.isKeyBoardUp

    在keyboardWillShow中: 如果 isKeyBoardUp 返回,则将其设置为 true 并执行您的操作。

    在键盘中将隐藏: 如果不是 isKeyBoardUp 返回,则将其设置为 false 并执行您的操作。

    【讨论】:

    • 当然,但我也不想把键盘放在文本字段下面
    【解决方案3】:

    请检查此代码,我正在尝试并适用于从

    获取此代码

    Move textfield when keyboard appears swift

     func keyboardWasShown(notification: NSNotification) {
       var info = notification.userInfo!
       var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
    
       UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.bottomConstraint.constant = keyboardFrame.size.height + 20
       })
     }
    

    【讨论】:

    • 我正在学习 cmets 但我不明白什么是约束
    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多