Search code examples
iphoneioscocoauiscrollview

Keep subviews of UIScrollView from resizing?


I have a UIScrollView, with several levels of subviews of the view returned by viewForZoomingInScrollView. During zooming, I want some of those subviews to resize, and others to not resize. No matter what I try, all subviews resize. On the superview of the subviews I want to not resize, I've set autoResizesSubviews = NO, and also tried contentMode = UIViewContentModeCenter. I've also tried setting the autoresizingMask for all views involved to UIViewAutoresizingNone. Any thoughts? I can't restructure the hierarchy of subviews.


Solution

  • The subviews of UIScrollView's zooming view are "resized" by changing their transforms, not by changing their bounds, etc. One simple solution to your question is to catch the transform being set on the zooming view, invert it, and apply it to the subviews, canceling out the zooming transform applied by the scroll view.

    In the view returned by viewForZoomingInScrollView, override -setTransform:, and go through all the subviews in question and apply the inverted transform:

    - (void)setTransform:(CGAffineTransform)transform 
    {
        [super setTransform:transform];
    
        CGAffineTransform invertedTransform = CGAffineTransformInvert(transform);
        for (UIView *view in subviewsNotToBeResized) 
        {
            [view setTransform:invertedTransform];
        }
    }