自动调整多个子视图

时间:2015-01-21 10:43:09

标签: ios uiview subview autoresizingmask

我有一个View说mainView。我已经为它设置了autoresizeMask属性。

[mainView autoresizingMask];
[mainView setAutoresizingMask : UIViewAutoresizingFlexibleWidth];
[mainView setAutoresizesSubviews:YES];

mainView会在方向更改时调整大小。

我想在mainView中有多个子视图,当mainView调整大小时应调整大小。

CGFloat width = mainView.frame.size.width/options.count;
for (int i = 0; i < options.count ; i++) {

    UIView *subView = [[UIView alloc] init];
    [subView setBackgroundColor:[ar objectAtIndex:i]];
    [subView setFrame:CGRectMake(i*width, 0, width, 40)];
    [self addSubview:subView];
    [subView autoresizingMask];
    [subView setAutoresizingMask : UIViewAutoresizingFlexibleWidth];
    [subView setAutoresizesSubviews:YES];

}

子视图在方向更改时不会调整其宽度。

此外,我希望每个子视图中的单个视图始终保持在相应的子视图的中心

for (int i = 0; i < options.count ; i++) {

        UIView *subView = [[UIView alloc] init];
        [subView setBackgroundColor:[ar objectAtIndex:i]];
        [subView setFrame:CGRectMake(i*width, 0, width, 40)];
        [self addSubview:subView];
        [subView autoresizingMask];
        [subView setAutoresizingMask : UIViewAutoresizingFlexibleWidth];
        [subView setAutoresizesSubviews:YES];

        UIView *childView = [[UIView alloc] init];
        [childView autoresizingMask];
        [childView setAutoresizingMask : UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
        childView.frame = CGRectMake(0, 0, 30, 30);
        childView.backgroundColor = [UIColor redColor];
        [subView addSubview:childView];
        childView.center = subView.center;


    }

我无法在旋转时达到预期的效果。 有人能告诉我怎样才能达到上述要求?

0 个答案:

没有答案