ContentMode scaleAspectToFit不起作用

时间:2011-06-25 09:39:31

标签: iphone resize scale contentmode

我有3个观看次数。 1.它是主视图,它包含另外两个视图。 这是一个比第一个更大的观点。 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];

1 个答案:

答案 0 :(得分:0)

我在这里更改了一个这样的视图 - 您必须修改所有视图

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

    [blueView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight];


    [blueView setContentMode:UIViewContentModeScaleAspectFit];