2016-11-27 4 views
1

화면 하단에있는 swift 앱에 버튼이 있습니다. 그것에 대한 제약 조건은 다음과 같습니다내 앱에 키보드가 나타나면 버튼이 부분적으로 위로 움직입니다 (버튼 텍스트 만 움직입니다).

enter image description here

I 화면 하단에서 내 버튼을 분리하는 제한 조건도 첨부 콘센트 : 나는 응용 프로그램을 실행하면

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 - 모두 keyboardWillAppear에서 endFrame.origin.y과 그렇지 않은 사라 내게 잘 어울려. 대신 키보드에서 제약 조건을 사용하려면 다음을 수행하십시오. continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant + keyBoardHeight 및 willDisappear continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant - keyBoardHeight. – Dili

+0

Apple이 선호하는 방법은 키보드가 표시된 경우에 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

관련 문제