gpt4 book ai didi

ios - 使用 NSLayoutConstraint 以编程方式为 iPhone 4 和 iPhone 5 使用一个背景图像

转载 作者:行者123 更新时间:2023-12-01 19:01:31 25 4
gpt4 key购买 nike

我试图通过使用 NSLayoutConstraint 来避免检测我使用的是 iPhone 4 还是 5 屏幕尺寸。 .这是我的代码:

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Flow-02"]];
[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[backgroundImage]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(backgroundImage)]];

但它不起作用。

我的 Flow-02 图像的分辨率为 640x1136,所以我想这会使它适合屏幕,但事实并非如此。有没有办法做到这一点?

编辑:在下面 rmaddy 的回答之后,我添加了设置图像框架的行,如下所示:
backgroundImage.frame = self.view.bounds;

它确实在 iPhone 5 模拟器上正确设置了我的图像,但在我的 iPhone 4s 上它仍然横向拉伸(stretch)。
autoresizingMask方法,也不是 V 的设置和 HconstraintsWithVisualFormat ,由 Anindya Sengupta 建议,有任何效果。

最佳答案

另一种选择是这样的:

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Flow-02"]];
backgroundImage.contentMode = UIViewContentModeScaleToFill;
backgroundImage.frame = self.view.bounds;
backgroundImage.autoResizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];

请记住,任何一种方法都只是在根据需要缩放时图像看起来不错的一个好主意。

关于ios - 使用 NSLayoutConstraint 以编程方式为 iPhone 4 和 iPhone 5 使用一个背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22536213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com