Can someone show me how to update the below code to work with iPhone 5, 5s, 6, and 6 +? When I test the simulator for the aforementioned devices, no image is set as the background...
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 == 480) {
// iPhone, iPod Touch
Image = [UIImage imageNamed:#"image.png"];
}
if ([[UIScreen mainScreen] bounds].size.height == 568) {
// supposably iPhone 5 but only works for 4
Image = [UIImage imageNamed:#"image.png"];
}
if ([[UIScreen mainScreen] bounds].size.height == 1024) {
// iPad
Image = [UIImage imageNamed:#"image.png"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:Image];
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
Then compare in your if statement.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:_Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
UIImage *Image = [[UIImage alloc]init];
if(IS_IPHONE_5)
{
Image = [UIImage imageNamed:#"image.png"];
}
if(IS_IPHONE_6)
{
Image = [UIImage imageNamed:#"image.png"];
}
if(IS_IPHONE_6P)
{
Image = [UIImage imageNamed:#"image.png"];
}
I don't see why you're doing it this way though.... Is it because your image is becoming distorted based on different devices? Because there are easier and more efficient ways to solve this in IB.
Related
I am trying to size my app for iPhone X and am having trouble.
Currently I define screen sizes like so:
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
and constrain based on the phone type. (Yes i know not optimal but its what ive done)
When you use the iPhone X simulator, for some reason it reads its screen height and width like an iPhone 6.
So then I searched stack overflox and found this:
How to get device make and model on iOS?
Only issue is, when you run on the iPhone X simulator it returns x86-64 and not a device type!
I obviously dont have an iPhone X so how do I detect that this is an iPhone X Simulator and not an iPhone 6 (based on screen size) so I can constrain properly for this new phone!
Thank you people!
func modelIdentifier() -> String {
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier }
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
from here
Objective-c:
- (NSString *)modelIdentifier {
NSString *simulatorModelIdentifier = [NSProcessInfo processInfo].environment[#"SIMULATOR_MODEL_IDENTIFIER"];
NSLog(#"%#",simulatorModelIdentifier);
if (simulatorModelIdentifier) {
return simulatorModelIdentifier;
}
struct utsname sysInfo;
uname(&sysInfo);
return [NSString stringWithCString:sysInfo.machine encoding:NSUTF8StringEncoding];
}
Don't forget:
#import <sys/utsname.h>
Use: (In viewDidLoad for example):
NSString *model = [self modelIdentifier];
NSLog(#"Device: %#", model);
If model is iPhone10,3, it is a iPhone X.
I don't recommend doing it this way, but if you must, the following code will return "iPhone10,3" or "iPhone10,6" for an iPhoneX.
NSString *GetHardwareName(void)
{
NSString *result = nil;
struct utsname platform;
if ( uname(&platform) == 0 )
{
if ( platform.machine[0] )
result = [NSString stringWithCString:platform.machine encoding:NSUTF8StringEncoding];
}
return result;
}
Hi you can use below micro to check iPhoneX size.
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_IPHONE_X (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0)
#define IS_IPAD_DEVICE [(NSString*)[UIDevice currentDevice].model hasPrefix:#"iPad"]
How can I detect whether a user has an iPhone 6 Plus in standard or zoomed mode?
Is this possible?
I've tried [UIScreen mainScreen].scale and it reports 3.0 in both cases.
There's a new member
[[UIScreen mainScreen] nativeScale]
which should do what you want. It's only available on iOS 8, so you'll need to guard it
[UIScreen mainScreen].currentMode reports:
<UIScreenMode: 0x17802f240; size = 1242.000000 x 2208.000000> // STANDARD
<UIScreenMode: 0x178226be0; size = 1125.000000 x 2001.000000> // ZOOMED
WORKING SOLUTION:
//Display Zoom mode
var isZoomed: Bool {
return UIScreen.main.scale < UIScreen.main.nativeScale
}
OLD SOLUTION (not reliable in some edge case):
var isZoomed: Bool {
return UIScreen.main.scale != UIScreen.main.nativeScale
}
P.S: Please do not confuse these features from the iPhone settings:
Settings -> Display & Brightness -> Display Zoom
with:
Settings -> Accessibility -> Zoom.
Here we check for the first type.
The following code may be used to get bounds, coordinateSpace, nativeScale and scale, i.e. on an iPhone 6 Plus the nativeScale is 2.608 and when the device in run in Zoomed Mode it is 2.88 (note that it is different in the simulator):
UIScreen *mainScreen = [UIScreen mainScreen];
NSLog(#"Screen bounds: %#, Screen resolution: %#, scale: %f, nativeScale: %f",
NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);
Code for detecting iPhone 6 Plus:
-(BOOL)iPhone6PlusDevice{
// Scale is 3 currently only for iPhone 6 Plus
if ([UIScreen mainScreen].scale > 2.9) return YES;
return NO;
}
or
-(BOOL)iPhone6PlusUnZoomed{
if ([self iPhone6PlusDevice]){
if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES; // Height is 736, but 667 when zoomed.
}
return NO;
}
Note: If you are checking for iPhone 6 Plus, to adjust the user interface then don´t rely on .nativeScale, because the simulator and an actual device give different results.
Updated macros from Paula Chavarría's answer for iOS 8 (where [UIScreen mainScreen].bounds.size is orientation dependent):
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && (MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER))
#define IS_STANDARD_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale)
#define IS_ZOOMED_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale > [UIScreen mainScreen].scale)
#define IS_STANDARD_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 736.0)
#define IS_ZOOMED_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale < [UIScreen mainScreen].scale)
#define IS_IPHONE_6 (IS_STANDARD_IPHONE_6 || IS_ZOOMED_IPHONE_6)
#define IS_IPHONE_6_PLUS (IS_STANDARD_IPHONE_6_PLUS || IS_ZOOMED_IPHONE_6_PLUS)
These option is used to detect iPhone Devices.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0)
I'm new to iOS programming. I want to ask these questions about image sizes for different screen sizes
Do x, 2x and 3x image sizes suffice for all the iOS devices? I mean if I have an image named "background.png", will background.png, background2x.png and background3x.png be sufficient for all the iOS devices/screen sizes?
If not, do different iPad models require some other image sizes(other than x, 2x and 3x)? ....... A link for explaining image sizes for different screen sizes/devices will be appreciated. Thanks
yes x, 2x and 3x image sizes is necessary to develop application for all iOS devices , but the size of images can be different for iPad & iPhone devices, depending on your application UI for iPad & iPhone .
For better understanding for background images please have a look Adaptivity and Layout
One of the best article related to your query - Adaptive Layout Tutorial in iOS 9: Getting Started
If you made images for #1x, #2x, and #3x, name them like this:
iPhone 3: myImage.png
iPhone 4, 4S: myImage#2x.png
iPhone 5, 5S: myImage-568h#2x.png
iPhone 6, 6S: myImage-667h#2x.png
iPhone 6P, 6PS : myImage-736h#3x.png
Then you can just call the blow method like:
UIImage *myImage = [self deviceSizedImageWithName:#"myImage.png"];
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenSize [UIScreen mainScreen].bounds.size
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4 (IS_IPHONE && kScreenHeight == 480.0f)
#define IS_IPHONE_5 (IS_IPHONE && kScreenHeight == 568.0f)
#define IS_IPHONE_6 (IS_IPHONE && kScreenHeight == 667.0f)
#define IS_IPHONE_6P (IS_IPHONE && kScreenHeight == 736.0f)
- (UIImage *)deviceSizedImageWithName:(NSString *)imageNamed
{
NSString *imgExtension = [imageNamed pathExtension];
NSString *imgName = [imageNamed stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#".%#", imgExtension] withString:#""];
BOOL removedExt = [imgExtension length];
UIImage *image = [UIImage imageNamed:imageNamed];
if (IS_IPHONE_5) {
if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-568h.%#", imgName, imgExtension]];
else image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-568h", imageNamed]];
if (!image) return [UIImage imageNamed:imageNamed];
} else if (IS_IPHONE_6) {
if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-667h.%#", imgName, imgExtension]];
else image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-667h", imageNamed]];
if (!image) return [UIImage imageNamed:imageNamed];
} else if (IS_IPHONE_6P) {
if (removedExt) image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-736.%#", imgName, imgExtension]];
else image = [UIImage imageNamed:[NSString stringWithFormat:#"%#-736", imageNamed]];
if (!image) return [UIImage imageNamed:imageNamed];
}
return image;
}
How can I detect whether a user has an iPhone 6 Plus in standard or zoomed mode?
Is this possible?
I've tried [UIScreen mainScreen].scale and it reports 3.0 in both cases.
There's a new member
[[UIScreen mainScreen] nativeScale]
which should do what you want. It's only available on iOS 8, so you'll need to guard it
[UIScreen mainScreen].currentMode reports:
<UIScreenMode: 0x17802f240; size = 1242.000000 x 2208.000000> // STANDARD
<UIScreenMode: 0x178226be0; size = 1125.000000 x 2001.000000> // ZOOMED
WORKING SOLUTION:
//Display Zoom mode
var isZoomed: Bool {
return UIScreen.main.scale < UIScreen.main.nativeScale
}
OLD SOLUTION (not reliable in some edge case):
var isZoomed: Bool {
return UIScreen.main.scale != UIScreen.main.nativeScale
}
P.S: Please do not confuse these features from the iPhone settings:
Settings -> Display & Brightness -> Display Zoom
with:
Settings -> Accessibility -> Zoom.
Here we check for the first type.
The following code may be used to get bounds, coordinateSpace, nativeScale and scale, i.e. on an iPhone 6 Plus the nativeScale is 2.608 and when the device in run in Zoomed Mode it is 2.88 (note that it is different in the simulator):
UIScreen *mainScreen = [UIScreen mainScreen];
NSLog(#"Screen bounds: %#, Screen resolution: %#, scale: %f, nativeScale: %f",
NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);
Code for detecting iPhone 6 Plus:
-(BOOL)iPhone6PlusDevice{
// Scale is 3 currently only for iPhone 6 Plus
if ([UIScreen mainScreen].scale > 2.9) return YES;
return NO;
}
or
-(BOOL)iPhone6PlusUnZoomed{
if ([self iPhone6PlusDevice]){
if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES; // Height is 736, but 667 when zoomed.
}
return NO;
}
Note: If you are checking for iPhone 6 Plus, to adjust the user interface then don´t rely on .nativeScale, because the simulator and an actual device give different results.
Updated macros from Paula Chavarría's answer for iOS 8 (where [UIScreen mainScreen].bounds.size is orientation dependent):
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && (MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER))
#define IS_STANDARD_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale)
#define IS_ZOOMED_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale > [UIScreen mainScreen].scale)
#define IS_STANDARD_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 736.0)
#define IS_ZOOMED_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale < [UIScreen mainScreen].scale)
#define IS_IPHONE_6 (IS_STANDARD_IPHONE_6 || IS_ZOOMED_IPHONE_6)
#define IS_IPHONE_6_PLUS (IS_STANDARD_IPHONE_6_PLUS || IS_ZOOMED_IPHONE_6_PLUS)
These option is used to detect iPhone Devices.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0)
Hi i would like to check iPhone Device Version in iOS.
I mean , currently running device is iPhone 4 or iPhone 5.
I need to check the device , is that iPhone 5 or not?
Because i have some problem in my app that need to know iPhone 5 or not.
So how can i?
Add this code:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)]) {
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 960) {
NSLog(#"iPhone 4 Resolution");
resolution_number = 1;
}
if(result.height == 1136) {
NSLog(#"iPhone 5 Resolution");
}
}
else{
NSLog(#"Standard Resolution");
}
}
Add This Macros to your code:
#define HEIGHT_IPHONE_5 568
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds ].size.height == HEIGHT_IPHONE_5 )
then just check whenever you needs..
if (IS_IPHONE_5) {
//Code for iPhone5
}else{
//Code for earlier version
}
I actually found a #define that does the trick
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double) 568) < DBL_EPSILON)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0f)