Binding a button (UIView) to keyboard that works in multiple textfields (Swift 4.2)











up vote
0
down vote

favorite












Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.



I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.



picture: Initial view without keyboard.



picture: keyboard has been launched.



My UIView extension:



import UIKit

extension UIView{
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}


@objc func keyboardWillChange(_ notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = endFrame.origin.y - beginningFrame.origin.y

UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
}


and I called bindToKeyboard() to loginBtn in the viewDidLoad() of my LoginVC like so:



loginBtn.bindToKeyboard()


The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.



The point of my question:




  1. how can I implement the extension to be able to work properly with multiple textfields/textviews?

  2. If that's not possible, how should I approach this problem?


I am sorry if my explanation and or English is unclear.



Thank you so much.










share|improve this question


























    up vote
    0
    down vote

    favorite












    Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.



    I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.



    picture: Initial view without keyboard.



    picture: keyboard has been launched.



    My UIView extension:



    import UIKit

    extension UIView{
    func bindToKeyboard(){
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }


    @objc func keyboardWillChange(_ notification: NSNotification){
    let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
    let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
    let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let deltaY = endFrame.origin.y - beginningFrame.origin.y

    UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
    self.frame.origin.y += deltaY
    }, completion: nil)
    }
    }


    and I called bindToKeyboard() to loginBtn in the viewDidLoad() of my LoginVC like so:



    loginBtn.bindToKeyboard()


    The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.



    The point of my question:




    1. how can I implement the extension to be able to work properly with multiple textfields/textviews?

    2. If that's not possible, how should I approach this problem?


    I am sorry if my explanation and or English is unclear.



    Thank you so much.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.



      I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.



      picture: Initial view without keyboard.



      picture: keyboard has been launched.



      My UIView extension:



      import UIKit

      extension UIView{
      func bindToKeyboard(){
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
      }


      @objc func keyboardWillChange(_ notification: NSNotification){
      let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
      let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
      let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
      let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
      let deltaY = endFrame.origin.y - beginningFrame.origin.y

      UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
      self.frame.origin.y += deltaY
      }, completion: nil)
      }
      }


      and I called bindToKeyboard() to loginBtn in the viewDidLoad() of my LoginVC like so:



      loginBtn.bindToKeyboard()


      The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.



      The point of my question:




      1. how can I implement the extension to be able to work properly with multiple textfields/textviews?

      2. If that's not possible, how should I approach this problem?


      I am sorry if my explanation and or English is unclear.



      Thank you so much.










      share|improve this question













      Sorry if any similar question(s) has been answered. But, I just can't seem to figure this one out.



      I have reached my goal, to bind the "Log In" button to the keyboard, basically pushing it from the bottom of the screen to the top of the keyboard using an extension below.



      picture: Initial view without keyboard.



      picture: keyboard has been launched.



      My UIView extension:



      import UIKit

      extension UIView{
      func bindToKeyboard(){
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
      }


      @objc func keyboardWillChange(_ notification: NSNotification){
      let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
      let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
      let beginningFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
      let endFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
      let deltaY = endFrame.origin.y - beginningFrame.origin.y

      UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
      self.frame.origin.y += deltaY
      }, completion: nil)
      }
      }


      and I called bindToKeyboard() to loginBtn in the viewDidLoad() of my LoginVC like so:



      loginBtn.bindToKeyboard()


      The problem here is, after the first tap to the textfield (either email or password field), the button disappears. After the keyboard is closed, the button is actually back to its initial position just like in the first picture. Then calling the keyboard again by tapping one of those textfields, the button works properly. But the second and so forth tap, it does not.



      The point of my question:




      1. how can I implement the extension to be able to work properly with multiple textfields/textviews?

      2. If that's not possible, how should I approach this problem?


      I am sorry if my explanation and or English is unclear.



      Thank you so much.







      ios swift uiview swift4.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 19 hours ago









      Sean Saoirse

      33




      33
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          How about try to implement same login button with keyboard inputAccessoryView?
          Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.






          share|improve this answer








          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – CGPA6.4
            4 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343726%2fbinding-a-button-uiview-to-keyboard-that-works-in-multiple-textfields-swift-4%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          How about try to implement same login button with keyboard inputAccessoryView?
          Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.






          share|improve this answer








          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – CGPA6.4
            4 hours ago















          up vote
          0
          down vote













          How about try to implement same login button with keyboard inputAccessoryView?
          Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.






          share|improve this answer








          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – CGPA6.4
            4 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          How about try to implement same login button with keyboard inputAccessoryView?
          Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.






          share|improve this answer








          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          How about try to implement same login button with keyboard inputAccessoryView?
          Then when keyboard shows up, the original login button will hidden by keyboard, but user will see another one on the top of keyboard.







          share|improve this answer








          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 18 hours ago









          HUNG IKAI

          1




          1




          New contributor




          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          HUNG IKAI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.








          • 1




            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – CGPA6.4
            4 hours ago














          • 1




            This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
            – CGPA6.4
            4 hours ago








          1




          1




          This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
          – CGPA6.4
          4 hours ago




          This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
          – CGPA6.4
          4 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343726%2fbinding-a-button-uiview-to-keyboard-that-works-in-multiple-textfields-swift-4%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]