简体   繁体   English

如何使用图像集为不同设备设置Viewcontroller背景图像?

[英]How to use Image Sets to set Viewcontroller background image for different device?

I have used background image for view controller in my previous applications. 在以前的应用程序中,我已将背景图像用作视图控制器。

That time I was using If..Else Ladder to detect the device on ViewDidload and based on that I was displaying different size of background image for the View controller. 那个时候,我使用If..Else Ladder在ViewDidload上检测设备,并据此为View控制器显示不同大小的背景图像。

Now I am using Images.XCAssets feature of Xcode. 现在,我正在使用Xcode的Images.XCAssets功能。 It is again works fine with LaunchImage, icons etc. but does not work with Background Image which is of viewcontroller size. 它再次适用于LaunchImage,图标等,但不适用于ViewController大小的Background Image。

Does anyone help me to use Image XCAsset for setting up view controller's background image? 有人帮助我使用Image XCAsset设置视图控制器的背景图像吗?

Thanks 谢谢

You can use this to set the different image sizes for the backgrounds or simply use a different image. 您可以使用此设置背景的不同图像尺寸,也可以仅使用其他图像。

NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width);
NSString *imageName = [NSString stringWithFormat:@"name-%@w", screenWidth];
UIImage *image = [UIImage imageNamed:imageName];

You can also check this answer for more info. 您也可以查看此答案以获取更多信息。

If you need to have different images for different devices you can simply add them in your image set. 如果需要为不同的设备使用不同的图像,则只需将它们添加到图像集中。

在此处输入图片说明

This way you have classic retina and Retina 4 (iPhone 5 and 6 I guess, since it's the same image ratio, 16/9) 这样,您就可以拥有经典的Retina和Retina 4(我猜是iPhone 5和6,因为它的图像比例相同,为16/9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM