Tab Bar scroll to the top of UIViewController when pressed twice










2















I'm trying to make my UITabBarController scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController on the storyboard that is linked to the code. Am I supposed to link UITabBar as well? Here's my code that I've attempted so far.



 import UIKit

class TabViewController: UITabBarController, UITabBarControllerDelegate
var pressedCount: Int = 0


override func viewDidLoad()
super.viewDidLoad()
self.delegate = self


// Do any additional setup after loading the view.

@IBAction func unwindToMain(segue: UIStoryboardSegue)

override func viewWillAppear(_ animated: Bool)

super.viewWillAppear(animated)

self.navigationController?.isNavigationBarHidden = true


override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
print("Selected item")


// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
print("Selected view controller")

func tabBarController(_ TabViewController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
guard let viewControllers = viewControllers else return false
if viewController == viewControllers[selectedIndex]
if let nav = viewController as? UINavigationController
guard let topController = nav.viewControllers.last else return true
if !topController.isScrolledToTop
topController.scrollToTop()
return false
else
nav.popViewController(animated: true)

return true



return true




extension UIViewController
func scrollToTop()
func scrollToTop(view: UIView?)
guard let view = view else return

switch view
case let scrollView as UIScrollView:
if scrollView.scrollsToTop == true
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return

default:
break


for subView in view.subviews
scrollToTop(view: subView)



scrollToTop(view: view)


var isScrolledToTop: Bool
if self is UITableViewController
return (self as! UITableViewController).tableView.contentOffset.y == 0

for subView in view.subviews
if let scrollView = subView as? UIScrollView
return (scrollView.contentOffset.y == 0)


return true











share|improve this question




























    2















    I'm trying to make my UITabBarController scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController on the storyboard that is linked to the code. Am I supposed to link UITabBar as well? Here's my code that I've attempted so far.



     import UIKit

    class TabViewController: UITabBarController, UITabBarControllerDelegate
    var pressedCount: Int = 0


    override func viewDidLoad()
    super.viewDidLoad()
    self.delegate = self


    // Do any additional setup after loading the view.

    @IBAction func unwindToMain(segue: UIStoryboardSegue)

    override func viewWillAppear(_ animated: Bool)

    super.viewWillAppear(animated)

    self.navigationController?.isNavigationBarHidden = true


    override func didReceiveMemoryWarning()
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
    print("Selected item")


    // UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    print("Selected view controller")

    func tabBarController(_ TabViewController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
    guard let viewControllers = viewControllers else return false
    if viewController == viewControllers[selectedIndex]
    if let nav = viewController as? UINavigationController
    guard let topController = nav.viewControllers.last else return true
    if !topController.isScrolledToTop
    topController.scrollToTop()
    return false
    else
    nav.popViewController(animated: true)

    return true



    return true




    extension UIViewController
    func scrollToTop()
    func scrollToTop(view: UIView?)
    guard let view = view else return

    switch view
    case let scrollView as UIScrollView:
    if scrollView.scrollsToTop == true
    scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
    return

    default:
    break


    for subView in view.subviews
    scrollToTop(view: subView)



    scrollToTop(view: view)


    var isScrolledToTop: Bool
    if self is UITableViewController
    return (self as! UITableViewController).tableView.contentOffset.y == 0

    for subView in view.subviews
    if let scrollView = subView as? UIScrollView
    return (scrollView.contentOffset.y == 0)


    return true











    share|improve this question


























      2












      2








      2








      I'm trying to make my UITabBarController scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController on the storyboard that is linked to the code. Am I supposed to link UITabBar as well? Here's my code that I've attempted so far.



       import UIKit

      class TabViewController: UITabBarController, UITabBarControllerDelegate
      var pressedCount: Int = 0


      override func viewDidLoad()
      super.viewDidLoad()
      self.delegate = self


      // Do any additional setup after loading the view.

      @IBAction func unwindToMain(segue: UIStoryboardSegue)

      override func viewWillAppear(_ animated: Bool)

      super.viewWillAppear(animated)

      self.navigationController?.isNavigationBarHidden = true


      override func didReceiveMemoryWarning()
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.

      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
      print("Selected item")


      // UITabBarControllerDelegate
      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
      print("Selected view controller")

      func tabBarController(_ TabViewController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
      guard let viewControllers = viewControllers else return false
      if viewController == viewControllers[selectedIndex]
      if let nav = viewController as? UINavigationController
      guard let topController = nav.viewControllers.last else return true
      if !topController.isScrolledToTop
      topController.scrollToTop()
      return false
      else
      nav.popViewController(animated: true)

      return true



      return true




      extension UIViewController
      func scrollToTop()
      func scrollToTop(view: UIView?)
      guard let view = view else return

      switch view
      case let scrollView as UIScrollView:
      if scrollView.scrollsToTop == true
      scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
      return

      default:
      break


      for subView in view.subviews
      scrollToTop(view: subView)



      scrollToTop(view: view)


      var isScrolledToTop: Bool
      if self is UITableViewController
      return (self as! UITableViewController).tableView.contentOffset.y == 0

      for subView in view.subviews
      if let scrollView = subView as? UIScrollView
      return (scrollView.contentOffset.y == 0)


      return true











      share|improve this question
















      I'm trying to make my UITabBarController scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController on the storyboard that is linked to the code. Am I supposed to link UITabBar as well? Here's my code that I've attempted so far.



       import UIKit

      class TabViewController: UITabBarController, UITabBarControllerDelegate
      var pressedCount: Int = 0


      override func viewDidLoad()
      super.viewDidLoad()
      self.delegate = self


      // Do any additional setup after loading the view.

      @IBAction func unwindToMain(segue: UIStoryboardSegue)

      override func viewWillAppear(_ animated: Bool)

      super.viewWillAppear(animated)

      self.navigationController?.isNavigationBarHidden = true


      override func didReceiveMemoryWarning()
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.

      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
      print("Selected item")


      // UITabBarControllerDelegate
      func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
      print("Selected view controller")

      func tabBarController(_ TabViewController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
      guard let viewControllers = viewControllers else return false
      if viewController == viewControllers[selectedIndex]
      if let nav = viewController as? UINavigationController
      guard let topController = nav.viewControllers.last else return true
      if !topController.isScrolledToTop
      topController.scrollToTop()
      return false
      else
      nav.popViewController(animated: true)

      return true



      return true




      extension UIViewController
      func scrollToTop()
      func scrollToTop(view: UIView?)
      guard let view = view else return

      switch view
      case let scrollView as UIScrollView:
      if scrollView.scrollsToTop == true
      scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
      return

      default:
      break


      for subView in view.subviews
      scrollToTop(view: subView)



      scrollToTop(view: view)


      var isScrolledToTop: Bool
      if self is UITableViewController
      return (self as! UITableViewController).tableView.contentOffset.y == 0

      for subView in view.subviews
      if let scrollView = subView as? UIScrollView
      return (scrollView.contentOffset.y == 0)


      return true








      swift xcode uiviewcontroller uitabbarcontroller uitabbar






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 4:47







      Nigel Denny

















      asked Nov 13 '18 at 3:13









      Nigel DennyNigel Denny

      697




      697






















          2 Answers
          2






          active

          oldest

          votes


















          1














          For example:



           var pressedCount: Int = 0

          func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)

          pressedCount += 1
          if pressedCount > 1
          scrollToTop(view: self.view)
          else
          //do something for first press







          share|improve this answer























          • Do I make a new controller for UITabBar or do I implement this into the rest of my code

            – Nigel Denny
            Nov 13 '18 at 4:40











          • try typing in func tabBarController(_ and look for didSelectItem

            – Alex Bailey
            Nov 13 '18 at 4:42












          • if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

            – Alex Bailey
            Nov 13 '18 at 4:43











          • I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

            – Nigel Denny
            Nov 13 '18 at 4:48











          • inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

            – Alex Bailey
            Nov 13 '18 at 4:49


















          0














          Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!






          share|improve this answer


















          • 1





            A little new to coding, could you give me an example of this?

            – Nigel Denny
            Nov 13 '18 at 3:59










          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%2f53273249%2ftab-bar-scroll-to-the-top-of-uiviewcontroller-when-pressed-twice%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          For example:



           var pressedCount: Int = 0

          func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)

          pressedCount += 1
          if pressedCount > 1
          scrollToTop(view: self.view)
          else
          //do something for first press







          share|improve this answer























          • Do I make a new controller for UITabBar or do I implement this into the rest of my code

            – Nigel Denny
            Nov 13 '18 at 4:40











          • try typing in func tabBarController(_ and look for didSelectItem

            – Alex Bailey
            Nov 13 '18 at 4:42












          • if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

            – Alex Bailey
            Nov 13 '18 at 4:43











          • I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

            – Nigel Denny
            Nov 13 '18 at 4:48











          • inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

            – Alex Bailey
            Nov 13 '18 at 4:49















          1














          For example:



           var pressedCount: Int = 0

          func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)

          pressedCount += 1
          if pressedCount > 1
          scrollToTop(view: self.view)
          else
          //do something for first press







          share|improve this answer























          • Do I make a new controller for UITabBar or do I implement this into the rest of my code

            – Nigel Denny
            Nov 13 '18 at 4:40











          • try typing in func tabBarController(_ and look for didSelectItem

            – Alex Bailey
            Nov 13 '18 at 4:42












          • if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

            – Alex Bailey
            Nov 13 '18 at 4:43











          • I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

            – Nigel Denny
            Nov 13 '18 at 4:48











          • inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

            – Alex Bailey
            Nov 13 '18 at 4:49













          1












          1








          1







          For example:



           var pressedCount: Int = 0

          func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)

          pressedCount += 1
          if pressedCount > 1
          scrollToTop(view: self.view)
          else
          //do something for first press







          share|improve this answer













          For example:



           var pressedCount: Int = 0

          func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)

          pressedCount += 1
          if pressedCount > 1
          scrollToTop(view: self.view)
          else
          //do something for first press








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 4:36









          Alex BaileyAlex Bailey

          382316




          382316












          • Do I make a new controller for UITabBar or do I implement this into the rest of my code

            – Nigel Denny
            Nov 13 '18 at 4:40











          • try typing in func tabBarController(_ and look for didSelectItem

            – Alex Bailey
            Nov 13 '18 at 4:42












          • if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

            – Alex Bailey
            Nov 13 '18 at 4:43











          • I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

            – Nigel Denny
            Nov 13 '18 at 4:48











          • inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

            – Alex Bailey
            Nov 13 '18 at 4:49

















          • Do I make a new controller for UITabBar or do I implement this into the rest of my code

            – Nigel Denny
            Nov 13 '18 at 4:40











          • try typing in func tabBarController(_ and look for didSelectItem

            – Alex Bailey
            Nov 13 '18 at 4:42












          • if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

            – Alex Bailey
            Nov 13 '18 at 4:43











          • I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

            – Nigel Denny
            Nov 13 '18 at 4:48











          • inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

            – Alex Bailey
            Nov 13 '18 at 4:49
















          Do I make a new controller for UITabBar or do I implement this into the rest of my code

          – Nigel Denny
          Nov 13 '18 at 4:40





          Do I make a new controller for UITabBar or do I implement this into the rest of my code

          – Nigel Denny
          Nov 13 '18 at 4:40













          try typing in func tabBarController(_ and look for didSelectItem

          – Alex Bailey
          Nov 13 '18 at 4:42






          try typing in func tabBarController(_ and look for didSelectItem

          – Alex Bailey
          Nov 13 '18 at 4:42














          if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

          – Alex Bailey
          Nov 13 '18 at 4:43





          if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.

          – Alex Bailey
          Nov 13 '18 at 4:43













          I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

          – Nigel Denny
          Nov 13 '18 at 4:48





          I've edited my code and I can now track when the tab bar is pressed, where would I implement your code

          – Nigel Denny
          Nov 13 '18 at 4:48













          inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

          – Alex Bailey
          Nov 13 '18 at 4:49





          inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 scrollToTop(view: self.view) else //do something for first press }

          – Alex Bailey
          Nov 13 '18 at 4:49













          0














          Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!






          share|improve this answer


















          • 1





            A little new to coding, could you give me an example of this?

            – Nigel Denny
            Nov 13 '18 at 3:59















          0














          Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!






          share|improve this answer


















          • 1





            A little new to coding, could you give me an example of this?

            – Nigel Denny
            Nov 13 '18 at 3:59













          0












          0








          0







          Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!






          share|improve this answer













          Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 3:46









          itzhangbaoitzhangbao

          1




          1







          • 1





            A little new to coding, could you give me an example of this?

            – Nigel Denny
            Nov 13 '18 at 3:59












          • 1





            A little new to coding, could you give me an example of this?

            – Nigel Denny
            Nov 13 '18 at 3:59







          1




          1





          A little new to coding, could you give me an example of this?

          – Nigel Denny
          Nov 13 '18 at 3:59





          A little new to coding, could you give me an example of this?

          – Nigel Denny
          Nov 13 '18 at 3:59

















          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%2f53273249%2ftab-bar-scroll-to-the-top-of-uiviewcontroller-when-pressed-twice%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto