2017-04-08 58 views
1

我遇到顯示鍵盤時更改約束的問題。 我目前正在建立一個登錄屏幕,我已經設置了約束,以在框架中水平和垂直居中一個具有2個文本框和一個按鈕的堆棧視圖。徽標(imageview)位於堆棧視圖之上,並具有獨立的約束。更改鍵盤動畫反彈的故事板約束

當鍵盤出現時,我想動畫的約束來移動stackview和標誌了一下,所以鍵盤不包括任何東西。

鍵盤在字段上還有一個「下一個」按鈕和一個「開始」按鈕。

問題我現在有的是,每次按下「下一步」或清除文本框,約束再次改變,從而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) 
} 

}

回答

0

這工作對我來說是隻需添加一個布爾值來檢查鍵盤是「主動」與否,它可能不是最乾淨的解決方案,但它解決了這一問題的解決方案,希望這有助於防止其他人遇到這種情況。