开发者

Auto resize subviews on device rotation

开发者 https://www.devze.com 2023-01-17 02:40 出处:网络
I have all my subviews set up so that they are based on self.view. EG: UIImageView *image = [[UIImageView alloc] initWithFrame开发者_JS百科:CGRectMake(0,0,(self.view.frame.size.width-20),(self.view.f

I have all my subviews set up so that they are based on self.view.

EG: UIImageView *image = [[UIImageView alloc] initWithFrame开发者_JS百科:CGRectMake(0,0,(self.view.frame.size.width-20),(self.view.frame.size.height-90))];

however when the view rotates (shouldRotateToDeviceOrientation or whatever) the views all stay the same size. How can I make them change shape to fit? Can I do this automatically?

Thanks


Absolutely. Take a look at the autoresizingMask property. If you set your image view, in this case, to have an autoresizing mask of UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight, then when its parent view resizes (as it may or may not automatically do when your app rotates—you might have to set a similar autoresizingMask on the parent view), it'll maintain the exterior margins you set up for it.


Just in case you have a view in the bottom of parent view this should help:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

Where self is your child view, that should be resized. If you do not set UIViewAutoresizingFlexibleTopMargin, then in horizontal mode you will not see your view, if it was in the bottom in vertical mode.

Also do not forget to set:

self.autoresizesSubviews = YES;

And autoresizing modes for all child view in your view. So that when it is resized, everything inside it also gets resized.


you have to [view setAutoresizingMask:...]

0

精彩评论

暂无评论...
验证码 换一张
取 消