简体   繁体   English

自定义UIView旋转时调整大小

[英]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; 我知道layoutSubviews ,但是1.旋转时没有调用它; 2. How I can resize a view here? 2.我如何在此处调整视图大小?

try to set autoresizingMask. 尝试设置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 您还可以查看autolayout

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

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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