Properly zooming a UIScrollView that contains many subviews

后端 未结 3 951
死守一世寂寞
死守一世寂寞 2020-12-14 03:05

I created a zoomable UIScrollView and added 100 subviews to it (tiled). The view scrolls perfectly left and right. However, I\'d like to allow zooming.

To do so I re

相关标签:
3条回答
  • 2020-12-14 03:13

    I created the view where I added everything using:

    UIView *zoomableView = [[UIView alloc] init];
    

    without setting its frame.

    The problem was solved when, after adding all the subviews to it, I set its frame to something large enough to accommodate all the tiled subviews.

    0 讨论(0)
  • 2020-12-14 03:15

    You have to return what your going to add views to scroll view as a subviews.

    Ex: If you are adding image view to scroll view then write

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
    {
    return imageView object;
    }
    
    0 讨论(0)
  • 2020-12-14 03:25

    Exactly,

    This is what Apple also is mentioning in the Scroll View Programming Guide:

    Just create a view, add all subviews to this view and add the newly created view as a single subview to the scrollview...

    Then in the viewForZoomingInScrollView delegate method return the object at Index 0:

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
    {
        return [self.scrollView.subviews objectAtIndex:0];
    }
    
    0 讨论(0)
提交回复
热议问题