gpt4 book ai didi

iphone - 关于自动调整蒙版

转载 作者:行者123 更新时间:2023-11-29 11:07:05 24 4
gpt4 key购买 nike

如果我有一个 superview.frame = CGRectMake(0,0,200,200); 和一个 subview.frame = CGRectMake(0,0,100,100), subview 具有灵活的宽度和高度,我认为当 superview 的 frame 变为 (0,0,150,150) 时,subview 的 frame 应该变为 (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);
}

输出:

0.0, 0.0, 50.0, 50.0

当父 View 的框架变为(0,0,100,100)时, subview 的框架将变为(0,0,0,0)

为什么..

最佳答案

用我的代码替换你的代码行,因为你忘记了 UIViewAutoresizingFlexibleBottomMarginUIViewAutoresizingFlexibleTopMargin

  view2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;

关于iphone - 关于自动调整蒙版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com