2017-12-30 12 views
0

내 textField가 FirstResponder가되어있을 때 버튼을 위로 이동하여 그대로 유지하려고합니다. 내 코드는 textField에서 타이핑을 시작하기 전까지 작동하고 버튼은 원래 위치, 즉 키보드 뒤쪽으로 되돌아갑니다. 같은스위프트 : 키보드 위의 버튼 이동 및 유지

내 코드 :

override func viewDidLoad() { 
    super.viewDidLoad() 
    view.addSubview(exchangeButton) 

    exchangeButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true 
    exchangeButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40).isActive = true 
    exchangeButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true 
    exchangeButton.heightAnchor.constraint(equalToConstant: 50).isActive = true 

    subscribeToShowKeyboardNotifications() 
    amountTextField.becomeFirstResponder() 

} 

@objc func keyboardDidShow(_ notification: Notification) { 
     let userInfo = notification.userInfo 
     let keyboardSize = userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue 
     let keyboardHeight = keyboardSize.cgRectValue.height 
     self.exchangeButton.frame.origin.y = self.exchangeButton.frame.origin.y - keyboardHeight 
    } 

func subscribeToShowKeyboardNotifications() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: .UIKeyboardDidShow, object: nil) 
} 

참고이 VC가있는 viewDidLoad에 becomeFirstResponder을 할당하여 출시 때 나는 즉시 becomeFirstResponder에 텍스트 필드를 싶습니다.

또한 UIKeyboardWillShow와 viewWillAppear을 사용하여 시도했습니다. 이러한 시나리오에서 내 버튼은 이전되지 않습니다.

내 참조 : this SO post

답변

0

당신이 가진 문제는 자동차를 사용하여 혼합되어있다 - 버튼을 수동으로 배치하려고하는 개미. 처음에는 viewDidLoad에 몇 가지 제약 조건을 설정하여 버튼의 위치와 크기를 설정합니다. 그런 다음 키보드가 나타나면 처음에 작동하는 것처럼 보이는 단추의 원점을 수동으로 변경합니다 (원하는 위치로 이동 함). 그러나 소유 뷰의 레이아웃이 업데이트되면 제약 조건이 다시 적용되어 버튼을 원래 위치로 되돌려 놓습니다. 텍스트를 입력하면 레이아웃을 강제로 수행 할 수 있습니다.

이 문제를 해결하려면 수동으로 프레임을 설정하는 대신 제약 조건을 조정해야합니다. 예제 테스트 클래스는 다음과 같습니다.

class ViewController: UIViewController { 
    @IBOutlet var testTextField: UITextField! 

    var testButton: UIButton! 
    var buttonConstraint: NSLayoutConstraint! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     testButton = UIButton(type: .custom) 
     testButton.backgroundColor = .green 
     self.view.addSubview(testButton) 
     testButton.translatesAutoresizingMaskIntoConstraints = false 
     testButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 

     testButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true 
     testButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40).isActive = true 

     buttonConstraint = testButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10) 
     buttonConstraint.isActive = true 

     testButton.heightAnchor.constraint(equalToConstant: 50).isActive = true 

     self.view.layoutIfNeeded() 
     subscribeToShowKeyboardNotifications() 
     testTextField.becomeFirstResponder() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @objc func keyboardWillShow(_ notification: Notification) { 
     let userInfo = notification.userInfo 
     let keyboardSize = userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue 
     let keyboardHeight = keyboardSize.cgRectValue.height 
     buttonConstraint.constant = -10 - keyboardHeight 
    } 

    @objc func keyboardWillHide(_ notification: Notification) { 
     buttonConstraint.constant = -10 
    } 

    func subscribeToShowKeyboardNotifications() { 
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil) 
    } 

    @objc func buttonAction() { 
     testTextField.resignFirstResponder() 
    } 
} 

이제 원본 위치를 -10으로 하드 코딩하는 것보다 더 나은 작업을 수행하는 것이 좋습니다. 그러나 이는 사용자의 책임입니다.

참고 모든 것을 좋게하려면 .UIKeyboardWillShow 및 .UIKeyboardWillHide를 사용했습니다. 또한 애니메이션을 적용하려면 다음을 수행하십시오.

@objc func keyboardWillShow(_ notification: Notification) { 
    let userInfo = notification.userInfo 
    let keyboardSize = userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue 
    let keyboardHeight = keyboardSize.cgRectValue.height 
    buttonConstraint.constant = -10 - keyboardHeight 

    let animationDuration = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double 
    UIView.animate(withDuration: animationDuration) { 
     self.view.layoutIfNeeded() 
    } 
} 

@objc func keyboardWillHide(_ notification: Notification) { 
    buttonConstraint.constant = -10 

    let userInfo = notification.userInfo 
    let animationDuration = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double 
    UIView.animate(withDuration: animationDuration) { 
     self.view.layoutIfNeeded() 
    } 
} 

키보드와 함께 움직이는 버튼을 움직이게합니다.