2017-12-30 11 views
0

私のtextFieldがfirstFirstResponderになったときにボタンを移動してそこに保持しようとしています。私のコードは、textFieldの入力を開始するまで機能し、ボタンは元の場所、つまりキーボードの背後に戻ります。スウィフト:キーボードの上にあるボタンを移動して滞在する

のような私のコード:このVCが起動し、これのviewDidLoadでbecomeFirstResponderを割り当てるされたとき、私はすぐにbecomeFirstResponderに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) 
} 

注意。

また、私はUIKeyboardWillShowとviewWillAppearも使用しようとしました。これらのシナリオでは、私のボタンは再配置されません。

マイ参照:this SO post

答えて

0

あなたが持っている問題Autoを使用して混合されていることですボタンを手動で配置しようとしています。最初に、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() 
    } 
} 

ボタンと一緒に動くボタンをアニメーション化します。

関連する問題