如果我有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)
为什么..
答案 0 :(得分:2)
在忘记UIViewAutoresizingFlexibleBottomMargin
和UIViewAutoresizingFlexibleTopMargin
view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
答案 1 :(得分:0)
在你的代码中,view2
有固定的边距,从顺时针到左上角是(0,100,100,0),这解释了view2
的自动调整行为。只有放大view2
时,view1
的高度和宽度才会改变。所以你应该将UIViewAutoresizingFlexibleBottomMargin
和UIViewAutoresizingFlexibleTopMargin
添加到view2
的autoresziing面具