0

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

Parth Pandya
  • 1,460
  • 3
  • 18
  • 34

2 Answers2

1

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.

Community
  • 1
  • 1
Jose Luis
  • 3,307
  • 3
  • 36
  • 53
  • This is the similar thing I tried in my previous application, I want to know if I can do it only from image.XCAsset – Parth Pandya Jul 30 '15 at 13:25
  • You have to specify the different images yourself in this case since it comes from the XCAsset/universal. – Jose Luis Jul 30 '15 at 13:28
  • This looks like it will break on any app using the iOS 9 multi-tasking enhancements. Your code assumes a set number of background sizes with images that have hardcoded names. Given that the width of the app can change in iOS 9 this looks like it will not work. The answer you have linked to even says `This might break if other widths are added in future`. – Robotic Cat Jul 30 '15 at 13:34
  • @Joze I see that scenario, but when we are using background image for button we just put **bold**@1x,@2x and @3x**bold** size. can't we do same like that for background image too – Parth Pandya Jul 30 '15 at 13:36
1

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)

KIDdAe
  • 2,714
  • 2
  • 22
  • 29
  • I tried it but it does not taking proper background image for different device. Still need to do some programming to set it up. – Parth Pandya Aug 01 '15 at 07:06