2017-01-28 3 views
1

特定のtextFieldが選択されている場合のみ、ビューを移動しようとしています。私はそれを動作させましたが、今度は他のtextFieldを選択すると、他のtextFieldでも再びアクティブになります、なぜですか?特定のtextFieldが選択されている場合にのみビューを移動します。

func keyboardWillShow(notification: NSNotification) { 

     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      if self.view.frame.origin.y == 0{ 
       self.view.frame.origin.y -= keyboardSize.height 
      } 
     } 

    } 

    func keyboardWillHide(notification: NSNotification) { 
     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      if self.view.frame.origin.y != 0{ 
       self.view.frame.origin.y += keyboardSize.height 
      } 
     } 
    } 

そして、このように私は、TextField touchDownaddObserverにしようとしています:言葉で

@IBAction func didTapCertainTextField(_ sender: Any) { 
     NotificationCenter.default.addObserver(self, selector: #selector(CreateCardViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

    } 

フロー:私は移動して取り扱っております。このよう

は私を言うことができます5つのテキストフィールドを持つ(1,2,3,4,5)

3番目のtextFieldにオブザーバを追加します。私は最初にクリックし、ビューは移動せず、3回目はクリックし、1は再びクリックして移動します。どうして?テキストフィールドがクリックされたときにビューを移動させたくありません。

+0

@LeoDabusに適用できる汎用的な解決策を持っていることを書きましたああ..大丈夫。だから私はそれをtextField自体に加えるべきですか? –

答えて

0

あなたが探しているのは、textFieldが最初のレスポンダであるかどうかを知ることですが、キーボードが表示されているときに通知します。

私はココアのこのクラスに見ることをお勧め: https://developer.apple.com/reference/uikit/uiresponder

それとも、あなたの正確なCASにあなたを通してXcodeのインタフェースビルダ をEditingDidBeginとEditingDidEndにアクションをバインドするUITextFieldかをプログラム的にそれを行うことができますデリゲート https://developer.apple.com/reference/uikit/uitextfielddelegate

また、私はあなたがこのイベントを観察し始めていることに気付くことができます。 オブジェクトの寿命の終わりより前のある時点でremoveObserver:name:object:を呼び出す必要があります

0

私が正しく理解していれば、キーボードがUITextFieldを隠しているときだけビューを上げようとしています。その場合は、私は特にそのために、以下の拡張子を書いた:

extension UIViewController { 

    func pushViewForTextFields() { 
     // Push view according text field's position 
     NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 

    @objc func keyboardWillShow(_ sender: Notification) { 
     var currentEditingTextField: UITextField! 
     let keyboardHeight = UIScreen.main.bounds.maxY - ((sender as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height 

     // Find which text field is currently editing (O(n)) 
     for view in self.view.subviews as [UIView] { 
      if let tf = view as? UITextField { 
       if tf.isEditing { 
        currentEditingTextField = tf 
        break 
       } 
      } 
     } 

     if currentEditingTextField == nil || currentEditingTextField < 0 { 
      // no textfield found 
      return 
     } 

     // get absolute frame of the text field, regardless of its parent view 
     let globalFrame = (currentEditingTextField.superview?.convert(senderObject.frame, to: self.view))! 

     // the 30 below is completely arbitrary; change it to whatever you want to suit your needs. This is basically the distance you want between the top of the keyboard and the editing text field (30px) 

     if globalFrame.maxY + 30 > keyboardHeight { 
      self.view.frame.origin.y = -(globalFrame.maxY + 30 - keyboardHeight) 
     } 
     else {  // keyboard not covering the text field 
      self.view.frame.origin.y = 0 
     } 

    } 

    @objc func keyboardWillHide(_ sender: Notification) { 
     // move view back to normal position when editing is done 
     self.view.frame.origin.y = 0 
    } 

} 

その後、単にあなたのUITextFieldオブジェクトがオンになっている場合は、この拡張機能にのみ動作することをごUIViewController

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.pushViewForTextFields() 
} 

このようなノートでそれを使用しますメインのトップレベル UIView。つまり、サブビューに埋め込まれていない場合です。サブビュー内にテキストフィールドがある場合は、for-loopを展開してfor-loopを展開し、すべてのサブビュー(この場合はO(n^2))のすべてのビューをチェックする必要があります。

これは(UIViewの木とすべてをループで)最も効率的な方法ではないかもしれないが、私は、私は簡単にすべての私のUIViewController

関連する問題