2017-04-24 1 views
0

내보기 하단 제한을 키보드 높이와 같게 설정하려고합니다. VC는 VC의 수명주기 동안 항상 활성화되어 있습니다.키보드 높이에서 UIView 설정

하지만, 내 VC는 IB keyboardHeigthConstraint에서

가 바닥 레이아웃 가이드 상단 및 내보기의 제약을하다 (I은 GIF에서 가리 켰을 때) 이상한 방식으로 높이의 0

같음 변경 내 시야를로드 할 때
@IBOutlet weak var keyboardHeigthConstraint: NSLayoutConstraint! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     cardNumberTextField.becomeFirstResponder() 

     NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil) 
    } 

func handleKeyboardNotification(notification: NSNotification) { 

    if let userInfo = notification.userInfo { 
     let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 

     DispatchQueue.main.async { 
      self.keyboardHeigthConstraint.constant = keyboardFrame.size.height 
      self.view.layoutIfNeeded() 
     } 
    } 

이러한 이상한 동작은 어떻게 될 수 있습니까? 당신의 GIF 이미지 난 당신의 버튼을 점프 볼 수 있고 그 문제가 될 수 있습니다에서

enter image description here

+0

여기에 무슨 문제가 있습니까? – KKRocks

+0

로드하는 동안 내 뷰가 "점프"하는 모습을 볼 수 있습니까? 나는 그것이 이상하다는 것을 안다. – dand1

+0

우리의 답변이 도움이되는지 여부를 알면 좋을 것입니다. –

답변

0

. textView에서 버튼의 수직 구속 조건을 추가하면 버튼이 항상 그 위치에있게됩니다. 모든 장치를 지원하려면 장치 크기에 따라 해당 수직 제약 조건을 계산해야합니다.

1

코드에서 몇 가지 문제가있어 :

  • cardNumberTextField.becomeFirstResponder() 대신 viewWillAppear에서 호출되어야한다;
  • 키보드 알림을 구독하면 cardNumberTextField.becomeFirstResponder()으로 전화하십시오.
  • 키보드 알림 수신 거부 viewDidDisappear;
  • DispatchQueue.main.async에 랩핑하지 않고 제약 조건을 업데이트하십시오.

도움이 될 것 같습니다.

@IBOutlet weak var keyboardHeigthConstraint: NSLayoutConstraint! 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) 
    cardNumberTextField.becomeFirstResponder() 
} 

override func viewDidDisappear(_ animated: Bool) { 
    NotificationCenter.default.removeObserver(self) 
    super.viewDidDisappear(animated) 
} 

func handleKeyboardNotification(notification: NSNotification) { 
    guard let userInfo = notification.userInfo, 
     let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else { 

     return 
    } 

    let keyboardFrame = frameValue.cgRectValue 
    keyboardHeigthConstraint.constant = keyboardFrame.height 
    view.layoutIfNeeded() 
} 
1

또한 실제로 모든 대답은 위의 나,하지만 내 최종 변형에 대한 작업 솔루션을 제공하지 않았다

let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue 

    UIView.animate(withDuration: keyboardDuration, animations: { 
     DispatchQueue.main.async { 
      self.keyboardHeigthConstraint.constant = keyboardFrame.size.height 
      self.view.layoutIfNeeded() 
     }    
    }) 
0

와 높이를 애니메이션 할 수는 theese 모두의 협력이

을 answres입니다

핵심 문제는 아래쪽 제한을 ​​변경하기로 결정한 것입니다. 상단으로 이동했을 때 "점핑"이 사라졌습니다

override func viewWillAppear(_ animated: Bool) { 
     cardNumberTextField.becomeFirstResponder() 
     NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil) 
    } 

    override func viewDidDisappear(_ animated: Bool) { 
     NotificationCenter.default.removeObserver(self) 
     super.viewDidDisappear(animated) 
    } 


    func handleKeyboardNotification(notification: NSNotification) { 

     guard let userInfo = notification.userInfo, 
      let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue 
      else { return } 

     let duration:TimeInterval = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSNumber)?.doubleValue ?? 0 

     print(keyboardHeigthConstraint.constant) 
     UIView.animate(withDuration: duration, animations: { 
//hardcoded 116 - it is buttonHeight + navBarHeight 
      self.keyboardHeigthConstraint.constant = UIScreen.main.bounds.height - frameValue.cgRectValue.size.height - 116 
      self.view.layoutIfNeeded() 
     }) 
     print(keyboardHeigthConstraint.constant) 
    } 
+0

차갑다. 경우에 따라서는 자신의 답을 받아들이고 일을 분명히 할 수도 있습니다. –