ContentMode scaleAspectToFit 不起作用

发布于 2024-11-17 00:53:59 字数 1195 浏览 2 评论 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];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-11-24 00:53:59

这里我改变了一个这样的视图 - 你必须修改你的所有视图

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

    [blueView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight];


    [blueView setContentMode:UIViewContentModeScaleAspectFit];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文