简体   繁体   中英

Custom UIView resizing while rotating

I know that there are several similar questions, but I still didn't find a proper solution. I've just created a test project with a custom view:

@interface CustomView : UIView

//..

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor redColor]];

    }
    return self;
}

and View Controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CustomView *cv = [(CustomView *) [CustomView alloc] initWithFrame:self.view.frame];
    [self.view addSubview: cv];
}

In the Landscape Mode we can see:

在此输入图像描述

How to fix it? I know about layoutSubviews , but 1. it's not called while rotating; 2. How I can resize a view here?

try to set autoresizingMask.

CustomView *cv = [(CustomView *) [CustomView alloc] initWithFrame:self.view.frame];
cv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview: cv];

You can also have a look at autolayout

https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles/UnderstandingAutolayout.html

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM