简体   繁体   English

当键盘显示时,Swift ios移动文本字段

[英]Swift ios move text field when keyboard is shown

I am new to ios/swift and not sure what to do when it comes to text fields / keyboard. 我是ios / swift的新手,不知道在文本字段/键盘方面该怎么做。

When I click on a textfield the keyboard blocks/covers it, so I cant select it or any other text field bellow it. 当我单击文本字段时,键盘会阻止/覆盖它,所以我无法选择它或任何其他文本字段。

So what is the best solution for this? 那么什么是最好的解决方案呢? Beside wrapping everything in a scrollview. 除了在scrollview中包装所有内容。

I found this snippet but im not sure on how to implement it: 我找到了这个片段,但我不确定如何实现它:

func textFieldDidBeginEditing(textField: UITextField) {
            animateViewMoving(true, moveValue: 100)
    }
    func textFieldDidEndEditing(textField: UITextField) {
            animateViewMoving(false, moveValue: 100)
    }

    func animateViewMoving (up:Bool, moveValue :CGFloat){
        var movementDuration:NSTimeInterval = 0.3
        var movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration )
        self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
        UIView.commitAnimations()
    }

Or if someone has a good code example/library from github, please share :) 或者如果有人从github有一个很好的代码示例/库,请分享:)

Thanks in advance, 提前致谢,

First of all, you should register for two keyboard notifications: 首先,您应该注册两个键盘通知:

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

Then, you should embed your text fields in a UIView (let's call it inputView ). 然后,您应该将文本字段嵌入到UIView (我们将其inputView )。 Next, you should take a reference to the top (or bottom constraint) of the view. 接下来,您应该参考视图的顶部(或底部约束)。 Here's the example with the bottom constraint: 以下是具有底部约束的示例:

@IBOutlet weak var inputViewBottomConstraint: NSLayoutConstraint!

Next, implement the two observer functions: 接下来,实现两个观察器函数:

func keyboardWillShow(notification : NSNotification) {

    var keyboardSize = notification.userInfo?[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size

    previousConstant = self.inputViewBottomConstraint.constant
    self.inputViewBottomConstraint.constant = keyboardSize!.height

    UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
        self.layoutIfNeeded()
    })
}

func keyboardWillHide(notification : NSNotification) {

    self.inputViewBottomConstraint.constant = previousConstant
    self.layoutIfNeeded()
}

Swift 3.0 - If your textField inside UITableView Swift 3.0 - 如果您的textField在UITableView中

    NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector:#selector(youViewContorllerName.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

  func keyboardWillShow(notification : NSNotification) {

        let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size

        self.constraintTblBottom.constant = keyboardSize.height
        UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: { () -> Void in
            self.layoutIfNeeded()
        })
    }

    func keyboardWillHide(notification : NSNotification) {
        self.constraintTblBottom.constant = 0
        self.layoutIfNeeded()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当键盘在文本字段上时,ios移动滚动视图 - ios move scrollview when keyboard is over text field 显示键盘时获取当前文本字段 - Get current text field when keyboard was shown 显示键盘时Swift 3移动视图-错误的KeyboardSize - Swift 3 move View when keyboard is shown - wrong KeyboardSize 如何在swift3 ios中使用键盘返回键将一个文本字段移动到另一个文本字段 - How to move one text field to another using keyboard return key in swift3 ios iOS Swift 3在显示键盘时将TableView内容保持在正确的位置 - iOS Swift 3 Keep TableView content at the right position when keyboard is shown Swift,iOS-单击以显示文本字段时按钮是否向上移动? 然后回去吗? - Swift, iOS - have button move up when clicked to display text field? Then move back? Xamarin.iOS:像聊天窗口一样进行编辑时,尝试将底部工具栏(文本字段)移至键盘上方 - Xamarin.iOS: Try to move bottom toolbar(text field) above keyboard when editing like chat window 测试键盘在显示时是否隐藏文本字段 - Test whether keyboard hide a text field when it's shown 移动视图,以便键盘不隐藏文本字段 - Move view when so that keyboard does not hide text field 键盘弹出时如何向上移动文本字段 - How to move text field up when keyboard pops up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM