views:

426

answers:

3

Hello,

I have a UIScrollView on screen.

Scroll view contains a view in which are UIButtons.

The problem is that while scrolling the view, if I press any button in between my scroll view will either bounce to top or bottom of the screen, it means it will not remain at place where I have pressed the button.

A: 

If I understand this correctly, your buttons are inside the scrollview? And when pressed you want them to stop the scrolling or move the scrollview to a specific point? Such as the top or the bottom? If so, use myScrollView.contentOffset = CGPointMake(0, 0); and specify the x and y values you need. So (0, 0) in that example would be the top left.

You might also want to try [myScrollView setContentOffset:CGPointMake(0, 0) animated:YES]; if you want to animate this transition.

Alan Taylor
+1  A: 

Hello pratik!

I've reproduced the situation you've described in your question (at least I think so): a UIScrollView, a UIView inside the UIScrollView and an UIButton inside the UIView.

I tried taping touching the scroll view and moving my finger across the button and didn't get it to bounce to the top or anything like that. I also tried tapping the button after scrolling the scroll and it had no side effects too.

So my guess is that what you are dealing with is something like this: a UINavigationController with you UIScroll-UIView-UIButton UIViewController as a root view controller; after being tapped the UIButton pushes a new UIViewController to the UINavigationController; then you go back to the previous UIViewController in the UINavigationController stack and this is when you lose you scroll position.

If my guess is right than here's what you need to do.

Before pushing a new UIViewController save you scroll view's current content offset with the following code:

[[NSUserDefaults standardUserDefaults] setObject:NSStringFromCGPoint(theScrollView.contentOffset) forKey:@"ScrollViewLastContentOffset"];

And after you get back to you UIScrollView UIViewController use the following code in you viewWillAppear: method:

- (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 if ([defaults stringForKey:@"ScrollViewLastContentOffset"] != nil) {
  theScrollView.contentOffset = CGPointFromString([defaults stringForKey:@"ScrollViewLastContentOffset"]);
  [defaults removeObjectForKey:@"ScrollViewLastContentOffset"];
 }
}

Even if my guess wrong please do comment on my and Alan's answer and confirm/refute them for us to be able to help you. Would you mind also providing more details on your problem so we wouldn't need to guess what's going on exactly anymore.

Ivan Karpan
+2  A: 

Take a look at the UIScrollViewDelegate class. You want to disable the buttons while scrolling begins enable it when the scrolling is done.

David Sowsy
You're like some kind of a telepath guessing what the author really was talking about from the first shot... :)
Ivan Karpan
I wish. :) It was more a matter of "been there, done that" and didn't want to see someone else suffer with the problem.
David Sowsy