autoresize mask使视图宽度为0

时间:2016-11-10 16:29:43

标签: ios autolayout uikit autoresizingmask

在下面的代码中,我看到如果我只是将redView的自动调整大小设置为UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight,则在旋转屏幕后视图的宽度为0。

但是,如果我将其设为UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin,则行为与预期一致,视图宽度和高度与新屏幕的宽度和高度成比例缩放。

为什么不仅仅设置flexibleHeight和width还不够?

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 150)];
redView.backgroundColor = [UIColor redColor];

//This makes the width 0 after rotation
//redView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

//This works
redView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin;

[self.view addSubview:redView];

0 个答案:

没有答案