繁体   English   中英

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

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

我是ios / swift的新手,不知道在文本字段/键盘方面该怎么做。

当我单击文本字段时,键盘会阻止/覆盖它,所以我无法选择它或任何其他文本字段。

那么什么是最好的解决方案呢? 除了在scrollview中包装所有内容。

我找到了这个片段,但我不确定如何实现它:

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()
    }

或者如果有人从github有一个很好的代码示例/库,请分享:)

提前致谢,

首先,您应该注册两个键盘通知:

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

然后,您应该将文本字段嵌入到UIView (我们将其inputView )。 接下来,您应该参考视图的顶部(或底部约束)。 以下是具有底部约束的示例:

@IBOutlet weak var inputViewBottomConstraint: NSLayoutConstraint!

接下来,实现两个观察器函数:

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 - 如果您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM