Keep subviews of UIScrollView from resizing?

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.


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];
    }
}

Keep subviews of UIScrollView from resizing?, Keep subviews of UIScrollView from resizing? has ambiguous scrollable content height xcode 11 uiview sizetofit sizethatfits uiview size to fit content resize uiview​  Incorrect resizing of UIScrollView subviews during scrollView frame resizing I have a situation where I have a UIView subclass with a UIScrollView as a member. I animate the resizing of the UIView and resize the scrollview with it. The UIScrollView is an infinite image gallery (i.e. 3 Views that swap content in and out as pag


Swift 4.2 version of Andrew Madsen's excellent answer:

/* maintains subview size during zoom */
class NonResizingView: UIView {
    override var transform: CGAffineTransform {
        get {return super.transform}
        set {
            super.transform = newValue
            for v in subviews {
                v.transform = self.transform.inverted()
            }
        }
    }
}

Keep subviews of UIScrollView from resizing?, func viewForZooming(in scrollView: UIScrollView) -> UIView? {; return self.​zoomImageView; }; //keeps the image the  So I have programmatically added a scrollView and a bunch of subviews to it. Let me show you: override func viewDidLoad() { super.viewDidLoad() let screensize: CGRect = UIScreen.main.bounds let screenWidth = screensize.width let screenHeight = screensize.height scrollView = UIScrollView(frame: CGRect(x: 0, y: 120, width: screenWidth, height: screenHeight)) scrollView.contentSize = CGSize(width


UIScrollView zooming is done with a transform, which will ignore your content mode and auto resizing mask. One possible solution is to place the content that you don't want resized into a sibling view of the scroll view instead of a sub view. Another is to try to invert the transform in real time with a UIView animation (see Andrew Madsen's answer).

Subview is not resizing properly |Apple Developer Forums, sizeToFit(). Resizes and moves the receiver view so it just encloses its subviews. Availability Specific UIKit views resize themselves according to their own internal needs. In some cases, if a Resizing Behavior. var contentMode: UIView. Currently if I resize the UIView by changing the frame width to match the UIScrollView's frame width then the UIView subviews (the text fields) don't resize themselves according to the constraints setup in IB. The UIView just seems to get clipped. There is no horizontal scrolling so this aspect is working correctly.


This avoids having to subclass UIView and seems to work just as well. containerView is the view returned by viewForZoomingInScrollView:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    containerView.subviews.forEach({
        if <test if this subview should be immune from resizing> {
            $0.transform = containerView.transform.inverted()
        }
    })
}

sizeToFit(), I have a UIScrollView that contains several dynamically resizing subviews. Dude, you keep setting the frame of the scrollView to something completely  In the superview's layoutSubviews method, I calculated the needed size of each subview, then set the frames so the subviews are tiled vertically down the screen with a bit of space between them. When done, I set the height of the UIScrollView's content size to be the end of the last subview (origin.y + size.height).


UIScrollView content size clipping subviews when it shouldn't, I have got a UIScrollView with a UIImageView as subview. The contentSize of the ScrollView has been set to the size of the UIImageView. I implemented  Similarly, after switching to a new UITableView, the system had to recalculate the position of the main UIScrollView subviews and change the size of the horizontal UIScrollView container.


UIScrollView keep scroll rect while resizing, [iOS] How to Solve UIScrollView Constraint Ambiguity In this figure, we set constraints on a 150 x 150 subview where its leading/trailing/top/bottom must align with its 200 x 200 Instead, you have to apply both to prevent error! You have to assign both aligning and sizing constraints to Content View. In this UIScrollView tutorial, you’ll create an app similar to the default iOS Photos app and learn all about UIScrollView. You’ll learn how to: Use UIScrollView to zoom and view a very large image. Keep UIScrollView‘s content centered while zooming. Use UIScrollView for vertical scrolling with Auto Layout.


[iOS] How to Solve UIScrollView Constraint Ambiguity, What I find less well documented is how to implement a self-sizing Using fixed proportions for the child views keeps it simple but is not private func calculatePreferredSize() { let targetSize = CGSize(width: view.bounds.width, height: UIView. The child only concerns itself with laying out its subviews. The UIScrollView has been around since the beginning of iOS, and there have been many blog posts, Stack Overflow questions, and Apple documentation covering how to set up your content to scroll inside a UIScrollView with the old springs & struts layout system.