Search code examples
iosxcode4uiscrollviewuiscrollviewdelegate

Keeping the State of UIScrollView iOS


I am implementing UIScroller View into my iPhone App. I have around 50 button in my scrollview and once a user click a button the Action will take him/her to another UIViewController. It is working fine and everything is oki.

But once the user return to the scrollviewController its start from the first button again.

Can i keep the user to the last button he selected before he move to the NextViewController

e.g. If a user selected 35th button and went to another View and once he can back he should be still stay at 35th not the 1st button.


Solution

  • You can get the current scroll position of the scrollview:

    CGPoint scrollingPoint=scrollView.contentOffset;
    

    Save this value in a variable (maybe in your appDelegate) and later restore the scrolling position:

    scrollView.contentOffset=scrollingPoint;
    

    Hope this helps.