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

521 views Asked by At

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.

Now I am using Images.XCAssets feature of Xcode. It is again works fine with LaunchImage, icons etc. but does not work with Background Image which is of viewcontroller size.

Does anyone help me to use Image XCAsset for setting up view controller's background image?

Thanks

2

There are 2 answers

4
Jose Luis On

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.

1
KIDdAe On

If you need to have different images for different devices you can simply add them in your image set.

enter image description here

This way you have classic retina and Retina 4 (iPhone 5 and 6 I guess, since it's the same image ratio, 16/9)