UITabBarController always open page at the top of UIViewController










1















I recently implemented into my code a way to scroll to the top of UIViewController by tapping an icon in UITabBar twice. The code is located inside my UITabBarController code. It works great, however the unfortunate side effect I've found is that every time I open up a page on my app, it's now at the top of UIViewController instead of the place where I last left off. I'm sure there's an error somewhere in this code



 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)
pressedCount += 1
if pressedCount > 1
scrollToTop()
else
//do something for first press

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


























    1















    I recently implemented into my code a way to scroll to the top of UIViewController by tapping an icon in UITabBar twice. The code is located inside my UITabBarController code. It works great, however the unfortunate side effect I've found is that every time I open up a page on my app, it's now at the top of UIViewController instead of the place where I last left off. I'm sure there's an error somewhere in this code



     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)
    pressedCount += 1
    if pressedCount > 1
    scrollToTop()
    else
    //do something for first press

    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
























      1












      1








      1








      I recently implemented into my code a way to scroll to the top of UIViewController by tapping an icon in UITabBar twice. The code is located inside my UITabBarController code. It works great, however the unfortunate side effect I've found is that every time I open up a page on my app, it's now at the top of UIViewController instead of the place where I last left off. I'm sure there's an error somewhere in this code



       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)
      pressedCount += 1
      if pressedCount > 1
      scrollToTop()
      else
      //do something for first press

      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 recently implemented into my code a way to scroll to the top of UIViewController by tapping an icon in UITabBar twice. The code is located inside my UITabBarController code. It works great, however the unfortunate side effect I've found is that every time I open up a page on my app, it's now at the top of UIViewController instead of the place where I last left off. I'm sure there's an error somewhere in this code



       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)
      pressedCount += 1
      if pressedCount > 1
      scrollToTop()
      else
      //do something for first press

      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










      asked Nov 13 '18 at 16:26









      Nigel DennyNigel Denny

      697




      697






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Problem is, that when your ViewController disappear, it still has pressedCount property set to 2



          So to viewWillAppear add this line to reset this:



          pressedCount = 0


          also fix if statement in tabBar didSelect item to reset pressedCount every time user presses tabBar item twice



          if pressedCount > 1 
          scrollToTop()
          pressedCount = 0
          else
          //do something for first press






          share|improve this answer
























            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%2f53285375%2fuitabbarcontroller-always-open-page-at-the-top-of-uiviewcontroller%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














            Problem is, that when your ViewController disappear, it still has pressedCount property set to 2



            So to viewWillAppear add this line to reset this:



            pressedCount = 0


            also fix if statement in tabBar didSelect item to reset pressedCount every time user presses tabBar item twice



            if pressedCount > 1 
            scrollToTop()
            pressedCount = 0
            else
            //do something for first press






            share|improve this answer





























              2














              Problem is, that when your ViewController disappear, it still has pressedCount property set to 2



              So to viewWillAppear add this line to reset this:



              pressedCount = 0


              also fix if statement in tabBar didSelect item to reset pressedCount every time user presses tabBar item twice



              if pressedCount > 1 
              scrollToTop()
              pressedCount = 0
              else
              //do something for first press






              share|improve this answer



























                2












                2








                2







                Problem is, that when your ViewController disappear, it still has pressedCount property set to 2



                So to viewWillAppear add this line to reset this:



                pressedCount = 0


                also fix if statement in tabBar didSelect item to reset pressedCount every time user presses tabBar item twice



                if pressedCount > 1 
                scrollToTop()
                pressedCount = 0
                else
                //do something for first press






                share|improve this answer















                Problem is, that when your ViewController disappear, it still has pressedCount property set to 2



                So to viewWillAppear add this line to reset this:



                pressedCount = 0


                also fix if statement in tabBar didSelect item to reset pressedCount every time user presses tabBar item twice



                if pressedCount > 1 
                scrollToTop()
                pressedCount = 0
                else
                //do something for first press







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 '18 at 17:00

























                answered Nov 13 '18 at 16:31









                Robert DreslerRobert Dresler

                5,3141526




                5,3141526



























                    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%2f53285375%2fuitabbarcontroller-always-open-page-at-the-top-of-uiviewcontroller%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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線