Scroll to the top by second tapped to Bar Item











up vote
0
down vote

favorite












I need to scroll to the top when I tapped second time to the first BarButtonItem (Home). There are a lot of answers but none of them don't work for me. It's incredible but it is.
I have UITabBarControllerDelegate in my class, self.tabBarController?.delegate = self in viewDidLoad() and this in my TableViewController:



func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if self.tabBarController?.selectedIndex == 0 {
self.tableView.setContentOffset(CGPoint(x: 0, y: -60), animated: true)
}
}


It works everytime I tapped to the Home (index = 0), but I need to do this when I'm already on the Home screen (standart function for all social media to scroll to the top Feed by tapping Home). And how to set Y coordinate if I have a NavigationBar above?
Thanks










share|improve this question




























    up vote
    0
    down vote

    favorite












    I need to scroll to the top when I tapped second time to the first BarButtonItem (Home). There are a lot of answers but none of them don't work for me. It's incredible but it is.
    I have UITabBarControllerDelegate in my class, self.tabBarController?.delegate = self in viewDidLoad() and this in my TableViewController:



    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    if self.tabBarController?.selectedIndex == 0 {
    self.tableView.setContentOffset(CGPoint(x: 0, y: -60), animated: true)
    }
    }


    It works everytime I tapped to the Home (index = 0), but I need to do this when I'm already on the Home screen (standart function for all social media to scroll to the top Feed by tapping Home). And how to set Y coordinate if I have a NavigationBar above?
    Thanks










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need to scroll to the top when I tapped second time to the first BarButtonItem (Home). There are a lot of answers but none of them don't work for me. It's incredible but it is.
      I have UITabBarControllerDelegate in my class, self.tabBarController?.delegate = self in viewDidLoad() and this in my TableViewController:



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      if self.tabBarController?.selectedIndex == 0 {
      self.tableView.setContentOffset(CGPoint(x: 0, y: -60), animated: true)
      }
      }


      It works everytime I tapped to the Home (index = 0), but I need to do this when I'm already on the Home screen (standart function for all social media to scroll to the top Feed by tapping Home). And how to set Y coordinate if I have a NavigationBar above?
      Thanks










      share|improve this question















      I need to scroll to the top when I tapped second time to the first BarButtonItem (Home). There are a lot of answers but none of them don't work for me. It's incredible but it is.
      I have UITabBarControllerDelegate in my class, self.tabBarController?.delegate = self in viewDidLoad() and this in my TableViewController:



      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      if self.tabBarController?.selectedIndex == 0 {
      self.tableView.setContentOffset(CGPoint(x: 0, y: -60), animated: true)
      }
      }


      It works everytime I tapped to the Home (index = 0), but I need to do this when I'm already on the Home screen (standart function for all social media to scroll to the top Feed by tapping Home). And how to set Y coordinate if I have a NavigationBar above?
      Thanks







      ios swift uitabbarcontroller






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 10:09

























      asked Nov 22 at 9:38









      Stanislav Putilov

      298




      298
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          All you have to do is check if selected viewController is your view controller that has tableView you need to scroll to top. Now I'd recommend using scroll to instead of setting content offset because that may vary based on the device and other logic (like large title for example)



          Considering you have your custom tab bar controller that has all the tabs references you could do something like this:



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if viewController == myViewController {
          myViewController.tableView.scrollToRow(
          at: IndexPath(row: 0, section: 0),
          at: .top,
          animated: true)
          }
          }
          }





          share|improve this answer





















          • Thanks, works nice!
            – Stanislav Putilov
            Nov 22 at 10:25










          • @StanislavPutilov please accept the answer if it worked.
            – inokey
            Nov 22 at 11:02











          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%2f53427857%2fscroll-to-the-top-by-second-tapped-to-bar-item%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
          1
          down vote



          accepted










          All you have to do is check if selected viewController is your view controller that has tableView you need to scroll to top. Now I'd recommend using scroll to instead of setting content offset because that may vary based on the device and other logic (like large title for example)



          Considering you have your custom tab bar controller that has all the tabs references you could do something like this:



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if viewController == myViewController {
          myViewController.tableView.scrollToRow(
          at: IndexPath(row: 0, section: 0),
          at: .top,
          animated: true)
          }
          }
          }





          share|improve this answer





















          • Thanks, works nice!
            – Stanislav Putilov
            Nov 22 at 10:25










          • @StanislavPutilov please accept the answer if it worked.
            – inokey
            Nov 22 at 11:02















          up vote
          1
          down vote



          accepted










          All you have to do is check if selected viewController is your view controller that has tableView you need to scroll to top. Now I'd recommend using scroll to instead of setting content offset because that may vary based on the device and other logic (like large title for example)



          Considering you have your custom tab bar controller that has all the tabs references you could do something like this:



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if viewController == myViewController {
          myViewController.tableView.scrollToRow(
          at: IndexPath(row: 0, section: 0),
          at: .top,
          animated: true)
          }
          }
          }





          share|improve this answer





















          • Thanks, works nice!
            – Stanislav Putilov
            Nov 22 at 10:25










          • @StanislavPutilov please accept the answer if it worked.
            – inokey
            Nov 22 at 11:02













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          All you have to do is check if selected viewController is your view controller that has tableView you need to scroll to top. Now I'd recommend using scroll to instead of setting content offset because that may vary based on the device and other logic (like large title for example)



          Considering you have your custom tab bar controller that has all the tabs references you could do something like this:



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if viewController == myViewController {
          myViewController.tableView.scrollToRow(
          at: IndexPath(row: 0, section: 0),
          at: .top,
          animated: true)
          }
          }
          }





          share|improve this answer












          All you have to do is check if selected viewController is your view controller that has tableView you need to scroll to top. Now I'd recommend using scroll to instead of setting content offset because that may vary based on the device and other logic (like large title for example)



          Considering you have your custom tab bar controller that has all the tabs references you could do something like this:



          func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
          if viewController == myViewController {
          myViewController.tableView.scrollToRow(
          at: IndexPath(row: 0, section: 0),
          at: .top,
          animated: true)
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 10:01









          inokey

          1,060617




          1,060617












          • Thanks, works nice!
            – Stanislav Putilov
            Nov 22 at 10:25










          • @StanislavPutilov please accept the answer if it worked.
            – inokey
            Nov 22 at 11:02


















          • Thanks, works nice!
            – Stanislav Putilov
            Nov 22 at 10:25










          • @StanislavPutilov please accept the answer if it worked.
            – inokey
            Nov 22 at 11:02
















          Thanks, works nice!
          – Stanislav Putilov
          Nov 22 at 10:25




          Thanks, works nice!
          – Stanislav Putilov
          Nov 22 at 10:25












          @StanislavPutilov please accept the answer if it worked.
          – inokey
          Nov 22 at 11:02




          @StanislavPutilov please accept the answer if it worked.
          – inokey
          Nov 22 at 11:02


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53427857%2fscroll-to-the-top-by-second-tapped-to-bar-item%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)