2016-11-27 22 views
1

我在屏幕底部的swift應用程序中有一個按鈕。它約束是:當我的應用程序中出現鍵盤時,該按鈕部分向上移動(僅該按鈕的文本移動)

enter image description here

我對於從屏幕底部分開我的按鈕的約束也連接插座:

enter image description here

當我運行應用程序時,我看到了我按鈕(我添加了一些背景色,以便我的示例清晰可見):

enter image description here

現在,奇怪的事情發生了 - 當鍵盤顯露,按鈕上的文本向上移動,藍色的背景保持它是:

enter image description here

而且還按鈕的可見部分是不是點擊所有。

它是我的實現中的某種錯誤還是問題?

我的代碼是針對相當簡單:

@IBOutlet weak var continueUsernameBottomConstraint: NSLayoutConstraint! 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 


    NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillAppear), name: .UIKeyboardWillShow, object: nil) 

    NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillDisappear), name: .UIKeyboardWillHide, object: nil) 

} 


func tutorialKeyboardWillAppear(notification: NSNotification){ 
    print("KEYBOARD APPEARS") 
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 

    continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y 

    self.view.layoutIfNeeded() 
} 

func tutorialKeyboardWillDisappear(notification: NSNotification){ 

    print("KEYBOARD DISAPPEARS") 
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 

    continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y 

    self.view.layoutIfNeeded() 

} 
+0

continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y兩個keyboardWillAppear消失不看起來很棒。相反,如果你想在keyboardWillAppear中使用約束:continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant + keyBoardHeight並在willDisappear中繼續使用nameBottomConstraint.constant = continueUsernameBottomConstraint.constant --keyBoardHeight。 – Dili

+0

蘋果公司首選的方法是使用scrollView來顯示鍵盤的情況。或者在這種情況下,即使您可以將整個屏幕向上移動並將鍵盤追加到底部。 – Dili

回答

2

使用此

func tutorialKeyboardWillAppear(notification: NSNotification){ 
    print("KEYBOARD APPEARS") 
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 
    continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant + CGFloat(endFrame.height) 
    self.view.layoutIfNeeded() 
} 

func tutorialKeyboardWillDisappear(notification: NSNotification){ 

    print("KEYBOARD DISAPPEARS") 
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 
    continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant - CGFloat(endFrame.height) 
    self.view.layoutIfNeeded() 

} 
+0

感謝男人,現在工作,這很奇怪,爲什麼以前沒有工作,但是,是的,如果有其他解決方案,我不會想太多,爲什麼其他方法不工作...... – user3766930