質問

I have an image as a subview of a UIScrollView. I want the image to initially fit the screen's bounds using autoResizingMask and contentMode (UIViewContentModeAspectFit) property of UIView. When the scrollview's frame is changed to make room for the keyboard, I don't want the child image view to scale down to fit the smaller frame. I can't disable autoResizeSubviews on the scroll view when it is created because the child view must be re-sized once at the beginning. Right now I can turn off subview re-sizing when the keyboard appears and re-enable it when it dissapears. This seems to work fine but seems hackish. Is that an acceptable way to do it or is there a better solution? Thanks.

役に立ちましたか?

解決

Don't worry about the autoResizingMask since it's a subview of the UIScrollView.

The autoResizingMask on a UIView allows for the view to be automatically resized when its parent view's frame has been resized. In this scenario, it sounds like your scrollView's frame is being adjusted vertically to accommodate the keyboard's on screen frame. When the parent view's frame shrinks, so does your UIImageView, which means it works as designed if you're using UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight as the autoResizingMask of your UIImageView.

Instead of using autoResizingMask on your UIImageView, you should manually set its frame to be the same size as the UIScrollView's frame via the bounds property:

imageView.frame = scrollView.bounds;

Then let's set the scrollView's contentSize to be the same size as the imageView.

scrollView.contentSize = imageView.frame.size;

This way, your imageView should be the full size of your scrollView, and won't move if you adjust the size of the scrollView frame.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top