更改键盘动画弹跳的故事板约束

时间:2017-04-08 12:49:54

标签: swift xcode swift3 keyboard constraints

显示键盘时,我遇到了更改约束的问题。 我正在构建一个登录屏幕,我在其中设置了约束,以便在框架中水平和垂直地放置一个带有2个文本字段和按钮的堆栈视图。徽标(imageview)位于堆栈视图上方,具有单独的约束。

当键盘出现时,我想设置约束动画以将叠加视图和徽标向上移动一点,这样键盘就不会覆盖任何内容。

键盘上还有一个“下一个”按钮和一个“go”按钮。

问题我现在有的是,每当我按“下一步”或清除文本字段时,约束会再次改变,因此stackview和徽标“反弹”,这有点烦人。另外,我知道很多人可能会习惯使用scrollview,但这不是一个真正的选择。 我也有底部的按钮来创建一个帐户,所以我无法真正改变整个框架高度。

我猜测因为它正在调用“textField.resignFirstResponder()”,所以会调用“keyboardWillHide”通知,因此它会开始制作动画。

我在互联网上到处搜索并尝试了很多东西来解决它,但我现在真的没有想法和动力,所以我希望有人知道我在这里做错了什么。 提前谢谢!

class LoginVC: UIViewController, UITextFieldDelegate {

@IBOutlet weak var emailField: SkyFloatingLabelTextFieldWithIcon!
@IBOutlet weak var passwordField: SkyFloatingLabelTextFieldWithIcon!
@IBOutlet weak var signInBtn: TransitionSubmitButton!
@IBOutlet weak var contentCenterConstraint: NSLayoutConstraint!
@IBOutlet weak var logoTopConstraint: NSLayoutConstraint!
@IBOutlet weak var equalWidthConstraint: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()

    setupKeyboardButtons()
    addObservers()

    // using extension to hide keyboard when tapped around
    self.hideKeyboardWhenTappedAround()
}

func addObservers(){

        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(goToDifferentView), name: NSNotification.Name(rawValue: "LoginPressed"), object: nil)
}

func keyboardWillShow(_ notification: NSNotification){

    // calling layoutIfNeeded because otherwise text in textfield bounces when animating for some reason
    view.layoutIfNeeded()

    self.contentCenterConstraint.constant = -50
    self.equalWidthConstraint.constant = -30
    self.logoTopConstraint.constant = -10
    UIView.animate(withDuration: 0.5) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(_ notification: NSNotification){


    view.layoutIfNeeded()
    self.contentCenterConstraint.constant = 0
    self.equalWidthConstraint.constant = 0
    self.logoTopConstraint.constant = 10
    UIView.animate(withDuration: 0.5) {
        self.view.layoutIfNeeded()
    }
}

private func setupKeyboardButtons(){
    // textfield delegates instellen, dit dient om keyboard actions te kunnen doen
    self.emailField.delegate = self
    self.emailField.enablesReturnKeyAutomatically = true
    self.passwordField.delegate = self
    self.passwordField.enablesReturnKeyAutomatically = true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    switch(textField) {
    case self.emailField:
        self.passwordField.becomeFirstResponder()
        break
    case self.passwordField:

        signInBtn.sendActions(for: .touchUpInside)
        break
    default:
        textField.resignFirstResponder()
        break
    }
    return true
}

// remove observers
override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

}

1 个答案:

答案 0 :(得分:0)

对我有用的解决方案是只添加一个布尔值来检查键盘是否“活动”,它可能不是最干净的解决方案,但它解决了问题,希望这有助于万一其他人遇到此