【问题标题】:About autoresizing mask关于自动调整蒙版
【发布时间】:2012-10-30 18:31:58
【问题描述】:

如果我有一个superview.frame = CGRectMake(0,0,200,200);和一个subview.frame = CGRectMake(0,0,100,100),子视图的宽度和高度是灵活的,我认为当superview的框架更改为(0,0,150,150)时,子视图的框架应该更改为(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

当superview的frame变为(0,0,100,100)时,subview的frame会变为(0,0,0,0)

为什么..

【问题讨论】:

    标签: iphone ios uiview autoresizingmask


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢伙计。我现在添加 UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin 。抱歉,我曾经认为添加 UIViewAutoresizingFlexibleLeftMargin 意味着修复左侧位置.. 我阅读了文档,现在没关系..
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多