关于autoresizing mask

时间:2012-10-30 10:07:54

标签: iphone ios uiview autoresizingmask

如果我有superview.frame = CGRectMake(0,0,200,200);subview.frame = CGRectMake(0,0,100,100),则子视图具有灵活的宽度和高度,我认为当superview的帧更改为(0,0,150,150)时,子视图的帧应更改为{{1} }。但事实并非如此。

(0, 0, 75, 75)

输出:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIView * view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view1.backgroundColor = [UIColor blackColor];
[self.view addSubview:view1];

UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view2.backgroundColor = [UIColor redColor];
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[view1 addSubview:view2];

view1.frame = CGRectMake(0, 0, 150, 150);
[self printRect:view2.frame];
}

- (void) printRect:(CGRect)rect
{
NSLog(@"%0.1f, %0.1f, %0.1f, %0.1f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}

当superview的帧变为(0,0,100,100)时,子视图的帧将变为(0,0,0,0)

为什么..

2 个答案:

答案 0 :(得分:2)

在忘记UIViewAutoresizingFlexibleBottomMarginUIViewAutoresizingFlexibleTopMargin

的情况下,将我的代码行替换为我的代码
  view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;

答案 1 :(得分:0)

在你的代码中,view2有固定的边距,从顺时针到左上角是(0,100,100,0),这解释了view2的自动调整行为。只有放大view2时,view1的高度和宽度才会改变。所以你应该将UIViewAutoresizingFlexibleBottomMarginUIViewAutoresizingFlexibleTopMargin添加到view2的autoresziing面具