开发者

ContentMode scaleAspectToFit doesn't work

I have 3 views. 1. It's the main view and it contains the other two views. 2. It's a view that is bigger than the first. 3. It's is contained by the second view.

I want to resize the second (and obviously the third) to the size of the first view maintaining aspect ratio. I try to get this result setting contentMode to scaleToAspectFitt but it doesn't work.

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];


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜