Reputation: 381
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.
Upvotes: 3
Views: 3821
Reputation: 2612
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()
}
}
}
}
Upvotes: 1
Reputation: 21383
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];
}
}
Upvotes: 16
Reputation: 6382
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).
Upvotes: 0