Can you set contentInset more than once?












1















I have a tableview with table cells. Each cell has a textfield. I have the following code to prevent the bottom few cells from being blocked by the keyboard



@objc func keyboardWillShow(_ notification:Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
}


The keyboardWillHide function works as expected. However, when the keyboard is hidden, the table does bounce back down the the bottom, resulting in no extra whitespace being shown (which is what I want). What I don't like is how you can still scroll down on the table to the contentInset from where the keyboard was first shown. Is there a way to make it so that after the keyboard disappears, that you can't scroll down passed the bottom of the table?










share|improve this question





























    1















    I have a tableview with table cells. Each cell has a textfield. I have the following code to prevent the bottom few cells from being blocked by the keyboard



    @objc func keyboardWillShow(_ notification:Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
    tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
    }
    }
    @objc func keyboardWillHide(notification: NSNotification) {
    tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
    }


    The keyboardWillHide function works as expected. However, when the keyboard is hidden, the table does bounce back down the the bottom, resulting in no extra whitespace being shown (which is what I want). What I don't like is how you can still scroll down on the table to the contentInset from where the keyboard was first shown. Is there a way to make it so that after the keyboard disappears, that you can't scroll down passed the bottom of the table?










    share|improve this question



























      1












      1








      1








      I have a tableview with table cells. Each cell has a textfield. I have the following code to prevent the bottom few cells from being blocked by the keyboard



      @objc func keyboardWillShow(_ notification:Notification) {
      if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
      tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
      }
      }
      @objc func keyboardWillHide(notification: NSNotification) {
      tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
      }
      override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
      }


      The keyboardWillHide function works as expected. However, when the keyboard is hidden, the table does bounce back down the the bottom, resulting in no extra whitespace being shown (which is what I want). What I don't like is how you can still scroll down on the table to the contentInset from where the keyboard was first shown. Is there a way to make it so that after the keyboard disappears, that you can't scroll down passed the bottom of the table?










      share|improve this question
















      I have a tableview with table cells. Each cell has a textfield. I have the following code to prevent the bottom few cells from being blocked by the keyboard



      @objc func keyboardWillShow(_ notification:Notification) {
      if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
      tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
      }
      }
      @objc func keyboardWillHide(notification: NSNotification) {
      tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
      }
      override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillChangeFrameNotification, object: nil)
      }


      The keyboardWillHide function works as expected. However, when the keyboard is hidden, the table does bounce back down the the bottom, resulting in no extra whitespace being shown (which is what I want). What I don't like is how you can still scroll down on the table to the contentInset from where the keyboard was first shown. Is there a way to make it so that after the keyboard disappears, that you can't scroll down passed the bottom of the table?







      ios swift uitableview nsnotificationcenter uikeyboard






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 21:53









      Sh_Khan

      41.1k51125




      41.1k51125










      asked Nov 12 '18 at 21:31









      jh95jh95

      786




      786
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Replace



          keyboardFrameBeginUserInfoKey


          with



          keyboardFrameEndUserInfoKey




          @objc func keyboardWillShow(_ notification:Notification) {
          if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
          }
          }
          @objc func keyboardWillHide(notification: NSNotification) {

          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
          }
          override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillHideNotification, object: nil)

          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillShowNotification, object: nil)
          }





          share|improve this answer


























          • Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

            – jh95
            Nov 12 '18 at 21:39













          • use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

            – Sh_Khan
            Nov 12 '18 at 21:41













          • I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

            – jh95
            Nov 12 '18 at 21:44






          • 1





            Works great! Thanks so much! If you don't mind, could you explain what made that work?

            – jh95
            Nov 12 '18 at 21:47






          • 1





            first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

            – Sh_Khan
            Nov 12 '18 at 21:50













          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',
          autoActivateHeartbeat: false,
          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%2f53270396%2fcan-you-set-contentinset-more-than-once%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









          2














          Replace



          keyboardFrameBeginUserInfoKey


          with



          keyboardFrameEndUserInfoKey




          @objc func keyboardWillShow(_ notification:Notification) {
          if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
          }
          }
          @objc func keyboardWillHide(notification: NSNotification) {

          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
          }
          override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillHideNotification, object: nil)

          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillShowNotification, object: nil)
          }





          share|improve this answer


























          • Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

            – jh95
            Nov 12 '18 at 21:39













          • use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

            – Sh_Khan
            Nov 12 '18 at 21:41













          • I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

            – jh95
            Nov 12 '18 at 21:44






          • 1





            Works great! Thanks so much! If you don't mind, could you explain what made that work?

            – jh95
            Nov 12 '18 at 21:47






          • 1





            first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

            – Sh_Khan
            Nov 12 '18 at 21:50


















          2














          Replace



          keyboardFrameBeginUserInfoKey


          with



          keyboardFrameEndUserInfoKey




          @objc func keyboardWillShow(_ notification:Notification) {
          if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
          }
          }
          @objc func keyboardWillHide(notification: NSNotification) {

          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
          }
          override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillHideNotification, object: nil)

          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillShowNotification, object: nil)
          }





          share|improve this answer


























          • Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

            – jh95
            Nov 12 '18 at 21:39













          • use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

            – Sh_Khan
            Nov 12 '18 at 21:41













          • I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

            – jh95
            Nov 12 '18 at 21:44






          • 1





            Works great! Thanks so much! If you don't mind, could you explain what made that work?

            – jh95
            Nov 12 '18 at 21:47






          • 1





            first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

            – Sh_Khan
            Nov 12 '18 at 21:50
















          2












          2








          2







          Replace



          keyboardFrameBeginUserInfoKey


          with



          keyboardFrameEndUserInfoKey




          @objc func keyboardWillShow(_ notification:Notification) {
          if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
          }
          }
          @objc func keyboardWillHide(notification: NSNotification) {

          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
          }
          override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillHideNotification, object: nil)

          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillShowNotification, object: nil)
          }





          share|improve this answer















          Replace



          keyboardFrameBeginUserInfoKey


          with



          keyboardFrameEndUserInfoKey




          @objc func keyboardWillShow(_ notification:Notification) {
          if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
          }
          }
          @objc func keyboardWillHide(notification: NSNotification) {

          tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
          }
          override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillHide), name: UIApplication.keyboardWillHideNotification, object: nil)

          NotificationCenter.default.addObserver(self, selector: #selector(TransactionViewController.keyboardWillShow), name: UIApplication.keyboardWillShowNotification, object: nil)
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 21:44

























          answered Nov 12 '18 at 21:33









          Sh_KhanSh_Khan

          41.1k51125




          41.1k51125













          • Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

            – jh95
            Nov 12 '18 at 21:39













          • use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

            – Sh_Khan
            Nov 12 '18 at 21:41













          • I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

            – jh95
            Nov 12 '18 at 21:44






          • 1





            Works great! Thanks so much! If you don't mind, could you explain what made that work?

            – jh95
            Nov 12 '18 at 21:47






          • 1





            first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

            – Sh_Khan
            Nov 12 '18 at 21:50





















          • Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

            – jh95
            Nov 12 '18 at 21:39













          • use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

            – Sh_Khan
            Nov 12 '18 at 21:41













          • I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

            – jh95
            Nov 12 '18 at 21:44






          • 1





            Works great! Thanks so much! If you don't mind, could you explain what made that work?

            – jh95
            Nov 12 '18 at 21:47






          • 1





            first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

            – Sh_Khan
            Nov 12 '18 at 21:50



















          Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

          – jh95
          Nov 12 '18 at 21:39







          Do I use keyboardFrameBeginUserInfoKey in both functions or just one?

          – jh95
          Nov 12 '18 at 21:39















          use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

          – Sh_Khan
          Nov 12 '18 at 21:41







          use keyboardFrameEndUserInfoKey in keyboardWillShow no need to access keyboard height in keyboardWillHide as you don't even use it

          – Sh_Khan
          Nov 12 '18 at 21:41















          I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

          – jh95
          Nov 12 '18 at 21:44





          I have edited the two functions in my post. I am still having the same problem of scrolling extra whitespace after the keyboard disappears.

          – jh95
          Nov 12 '18 at 21:44




          1




          1





          Works great! Thanks so much! If you don't mind, could you explain what made that work?

          – jh95
          Nov 12 '18 at 21:47





          Works great! Thanks so much! If you don't mind, could you explain what made that work?

          – jh95
          Nov 12 '18 at 21:47




          1




          1





          first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

          – Sh_Khan
          Nov 12 '18 at 21:50







          first begin frame is less height than end frame , second you call keyboardWillHide and keyboardWillShow to the same listender name UIApplication.keyboardWillChangeFrameNotification look to UIApplication.keyboardWillHideNotification and UIApplication.keyboardWillShowNotification

          – Sh_Khan
          Nov 12 '18 at 21:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270396%2fcan-you-set-contentinset-more-than-once%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

          Full-time equivalent

          Bicuculline

          さくらももこ