0

I am trying to override different background imagery to run on my application. I used the code from this question; set background images for Iphone 3g, Iphone 4s and Iphone 5

Thankfully the code is simple. I also set these in the .m file just in case.

.h

@property (retain strong) UIImage* Image 

@property (retain, strong) UIImageView.

.m

@interface ViewController ()

@end

@implementation ViewController

//SYNTHESIZE JUST IN CASE

@synthesize backgroundImageView=backgroundImageView;
@synthesize Image=Image;

- (void)viewDidLoad

{

//SETS A UIIMAGEVIEW TO BE SET TO A CUSTOM UIIMAGE (JPG)

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];

//I EXCPECTED THIS NEXT LINE OF CODE TO WORK TO SET A SEPARATE BACKGROUND FOR IPAD...

UIImage *Image = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
    Image = [UIImage imageNamed:@"11@2x.jpg"];
}
else

//THIS ELSE STATEMENT IS THE IMAGE THAT ACTUALLY RASTERIZES FOR BOTH IPAD AND IPOD TEST DEVICES

{
    Image = [UIImage imageNamed:@"7@2x.jpg"];
}

self.view.backgroundColor = [UIColor colorWithPatternImage:Image];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

The override isn't working quite as expected. Only the else statement runs no matter what image i set in the if statement.

My question; are the dimensions (568) in the if statement correct to rasterize a separate image for iPad devices? If not please let me know, thanks.

Additional info: deployment target: 6.1 Additional info: i received an error at run time (even though it still ran) akin to something about a discrepancy between my nib file and story board. I am not using a nib though. ?

Community
  • 1
  • 1
user3720631
  • 53
  • 1
  • 10
  • 568 is the height of an iPhone 5. 768 is the height of an iPad – Paulw11 Jul 07 '14 at 01:44
  • Also you declare properties for your background view and image, but then use local variables rather than the properties. Additionally your `Image` property (which should be `image` - classes start with upper case, variables start with lower case) will be retained by the background view so you only need a weak property, not strong – Paulw11 Jul 07 '14 at 01:47
  • 768? Thank you I will try it ( I am using an iPad mini) – user3720631 Jul 07 '14 at 01:56
  • I will try a weak property also thx recompiling in progress – user3720631 Jul 07 '14 at 01:57

1 Answers1

1

Here are the correct values:

if ([[UIScreen mainScreen] bounds].size.height == 480) {
    // iPhone, iPod Touch
}

if ([[UIScreen mainScreen] bounds].size.height == 568) {
    // iPhone 5
}

if ([[UIScreen mainScreen] bounds].size.height == 750) {
    // iPhone 6
}

if ([[UIScreen mainScreen] bounds].size.height == 1024) {
    // iPad
}

if ([[UIScreen mainScreen] bounds].size.height == 1242) {
    // iPhone 6 Plus
}
  1. iPhone [UIScreen mainScreen] bounds] {{0, 0}, {320, 480}}
  2. iPhone 5 [UIScreen mainScreen] bounds] {{0, 0}, {320, 568}}
  3. iPhone 6 [UIScreen mainScreen] bounds] {{0, 0}, {750, 1334}}
  4. iPad [UIScreen mainScreen] bounds] {{0, 0}, {768, 1024}}
  5. iPhone 6 Plus [UIScreen mainScreen] bounds] {{0, 0}, {1242, 2208}}

Edit: Added iPhone 6 and 6 Plus to maintain this list as per Jules' comment, but do not recommend using this method to differentiate between phones or even pick the right image.


Proper Approach

Use an assets catalog.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • 1
    What about iphone 6 etc ? – Jules Oct 06 '15 at 07:48
  • @Jules: you make an excellent point. This solution is not scalable. I was purely responding to http://stackoverflow.com/questions/24602007/, but you do no want to go down that road. – SwiftArchitect Oct 06 '15 at 23:16
  • @SwiftArchitect yeah thanks, I have my own question on this http://stackoverflow.com/questions/32948931/xcode-7-asset-catalog-universal-device-background-image-support asset catalog doesn't currently seem able to do this ? – Jules Oct 07 '15 at 05:19