0

I have one UIImageView (building's floor map) and UIScrollView with gesture recognisers which allow me to scroll horizontally and vertically, zoom in/out. Everything works OK, but the building has two floors, so user should have the option to switch between them. I decided to use segmented control at the top of the map to provide this option.
If I put Segmented Control to the same UIScrollView, it scrolls vertically as well as horizontally. What I am asking is how to fix horizontal position of the Segmented Control, so it will be able to scroll only vertically with map.
I am trying to use this code to test, if it fixes the position of Segmented Control absolutely, but it seems to be that it doesn't.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect frame = _floorSelector.frame;
    frame.origin.y=50;
    frame.origin.x= 160;
    _floorSelector.frame = frame;

}

Where is the mistake? Thank you for replies!

Richard Topchii
  • 4,569
  • 3
  • 27
  • 70
  • Does the segmented control even have to be contained in the scroll view? Why don't you just add it over the top of the scroll view? – nhgrif Jun 01 '14 at 02:19
  • It might be an idea, but I am trying to get Segmented Control to be able to scroll only vertically, not horizontally. I attach the screenshot to make it more understandable. https://pp.vk.me/c617220/v617220757/bdbc/JByJl-T53Bk.jpg – Richard Topchii Jun 01 '14 at 02:22

1 Answers1

0

I think the issue here is you are misunderstanding how scrolling is implemented in a UIScrollView, and how things are placed in its coordinate space.

When a UIScrollView is scrolled, the frames of its subviews are not changed. Relative to the scrollview, they remain in the same position while the contentOffset and bounds of the scroll view changes. In order to make a subview of a scroll view appear static while the rest of it scrolls, you must change its frame within the scrollview as the bounds change.

With that in mind, what you are doing now is just setting the same frame on that subview repeatedly, which is the same as not doing anything.

It sounds like what you want to do is something like this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect frame = _floorSelector.frame;
    frame.origin.x = 160 + scrollView.contentOffset.x;
    _floorSelector.frame = frame;

}

Notice I do not change anything in the y axis because based on your question, the vertical scrolling doesn't need to be changed.

Dima
  • 22,982
  • 5
  • 52
  • 82