0

我有 3 个视图。1.它是主视图,它包含其他两个视图。2. 这是一个比第一个更大的视图。3. 它包含在第二个视图中。

我想将第二个(显然是第三个)调整为保持纵横比的第一个视图的大小。我尝试将此结果设置 contentMode 为 scaleToAspectFitt 但它不起作用。

UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
yellowView.autoresizesSubviews = YES;
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.autoresizesSubviews = YES;
yellowView.contentMode = UIViewContentModeScaleAspectFit;

UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0,0,350,400)];
blueView.autoresizesSubviews = YES;
blueView.contentMode = UIViewContentModeScaleAspectFit;
blueView.autoresizingMask =  UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight;

UIView *darkView = [[UIView alloc] initWithFrame:CGRectMake(200, 100, 40, 40)];
darkView.backgroundColor = [UIColor blackColor];
darkView.contentMode = UIViewContentModeScaleAspectFit;
darkView.autoresizingMask =  UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight;

[blueView addSubview:darkView];
[yellowView addSubview:blueView];
[self.view addSubview:yellowView];

[yellowView setNeedsLayout];

[darkView release];
[blueView release];
[yellowView release];
4

1 回答 1

0

Here I changed for one view like this - you have to modify all your views

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0,0,350,400)];

    [blueView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight];


    [blueView setContentMode:UIViewContentModeScaleAspectFit];
于 2011-06-25T12:56:58.313 回答