简体   繁体   中英

xcode convert iPhone to iPad

help please. my code that change UIImageView is:

- (void) viewDidLoad {

background.image = [UIImage imageNamed:@"myImage.png"];

}

It works good on iPhone and also on iPad but, how can I change "myImage.png" to "myImageHD.png" when I will run this app on my iPad???? I just need to show myImageHD if my app will run on iPad. IT CAN BE DONE WITH XIB FILE, BUT I NEED WITH CODE TO FIX IT. To detect if app run on iPad, and show myImageHD. Thanks.

Do this,

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   background.image = [UIImage imageNamed:@"myImage.png"];
} else { //UIUserInterfaceIdiomPad is for iPad
   background.image = [UIImage imageNamed:@"myImageHD.png"];
}

Another way to do this would be,

if ([(NSString*)[UIDevice currentDevice].model isEqualToString:@"iPad"] } { //As per the below comment, for iPad simulator the string will be "iPad simulator"
    background.image = [UIImage imageNamed:@"myImageHD.png"];
} else {
    background.image = [UIImage imageNamed:@"myImage.png"];
} 

UI_USER_INTERFACE_IDIOM() also can be used to check if the current device is iPad or iPhone. Instead of [[UIDevice currentDevice] userInterfaceIdiom] in the above if condition, you can use UI_USER_INTERFACE_IDIOM() also.

NSString *deviceType = [UIDevice currentDevice].model;

if([deviceType isEqualToString:@"iPhone"])
{
     // it's an iPhone
}

this will help you, but i dont think that it a good try using same XIB and changing the image only! regards

Try this one

 - (void) viewDidLoad {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        background.image = [UIImage imageNamed:@"myImageHD.png"];
    }
    else{
        background.image = [UIImage imageNamed:@"myImage.png"];
    }
}

Try to use below code

- (void) viewDidLoad {

if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
background.image = [UIImage imageNamed:@"myImageHD.png"];
}
else
background.image = [UIImage imageNamed:@"myImage.png"];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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