I am using code which shows a certain xib file depending on the device, at the moment 3 xibs, one for iPhone4, one for iPhone5 and one for iPad.
Since iOS8 / iPhone 6, the app does not work on anything higher than the 5s due to the fact the xib does not know what to load;
- (IBAction)btnTapForStart:(id)sender {
fromOtherClass=YES;
if([[UIScreen mainScreen]bounds].size.height == 568)
{
PlayMusicViewController *pmvc = [[PlayMusicViewController alloc]initWithNibName:#"XibForIPhone5" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:pmvc animated:YES];
}
else
{
PlayMusicViewController *pmvc = [[PlayMusicViewController alloc]init];
[self.navigationController pushViewController:pmvc animated:YES];
}
[appObject.audioPlayer stop];
}
If I change the value of 568 to higher it will work, but is there a way to combine it, for example, height is 568 OR iPhone6 size OR iPhone 6 plus size etc?
You probably should use auto-layout like Maddy says, but if you want to have code that works for specific devices, then you might want to implement this utility method. Then you can create as many xibs as you want and target them to specific devices. e.g.
if ([[Utilities deviceType] isEqualToString:#"iPhone Retina4"] || [[Utilities deviceType] isEqualToString:#"iPhone Retina35"] ) {
-- do something specific for these phones
}
I put this method in a Utilities class.
+ (NSString *)deviceType {
NSString *device = nil;
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat deviceScale = [UIScreen mainScreen].scale;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
device = #"iPhone Classic"; // Just in case it doesn't make it through the conditionals
// Classic has a resolution of 480 × 320
if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f ) {
device = #"iPhone Classic";
// Retina has a resolution of 960 × 640
} else if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f ) {
device = #"iPhone Retina35";
// Retina 4" has a resolution of 1136 x 640
} else if (screenSize.height == 568 || screenSize.width == 568 ) {
device = #"iPhone Retina4";
// iPhone 6 has a resolution of 1334 by 750
} else if (screenSize.height == 667 || screenSize.width == 667 ) {
device = #"iPhone 6";
// iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080
// Reported size is 736 x 414
} else if (screenSize.height == 736 || screenSize.width == 736 ) {
device = #"iPhone 6 Plus";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
device = #"iPad Classic"; // Just in case it doesn't make it through the conditionals
if(deviceScale == 1.0f) {
device = #"iPad Classic";
} else if (deviceScale == 2.0f) {
device = #"iPad Retina";
}
}
//NSLog(#"The device is %# scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width);
return device;
}
I did some research on enums and they are nice. They make the code a bit more readable, but mostly they allow the compiler to help you type and catch errors. Xcode will autocomplete your deviceType for you and will give you the error: Use of undeclared identifier if you try to use a value that isn’t defined. Here’s the code rewritten as an enum. I prefixed the values with LF but you should use whatever is appropriate for your project.
This is in my header file
// Devices as of Fall 2014
typedef NS_ENUM(NSInteger, LFdeviceType) {
LFDeviceTypePhoneClassic,
LFDeviceTypePhoneRetina3_5,
LFDeviceTypePhoneRetina4,
LFDeviceTypePhone6,
LFDeviceTypePhone6Plus,
LFDeviceTypePadClassic,
LFDeviceTypePadRetina,
};
And this is in my .m file.
m+ (NSInteger)deviceType {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat deviceScale = [UIScreen mainScreen].scale;
LFdeviceType device = LFDeviceTypePhoneClassic;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
device = LFDeviceTypePhoneClassic; // Just in case it doesn't make it through the conditionals
// Classic has a resolution of 480 × 320
if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f ) {
device = LFDeviceTypePhoneClassic;
// Retina has a resolution of 960 × 640
} else if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f ) {
device = LFDeviceTypePhoneRetina3_5;
// Retina 4" has a resolution of 1136 x 640
} else if (screenSize.height == 568 || screenSize.width == 568 ) {
device = LFDeviceTypePhoneRetina4;
// iPhone 6 has a resolution of 1334 by 750
} else if (screenSize.height == 667 || screenSize.width == 667 ) {
device = LFDeviceTypePhone6;
// iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080
// Reported size is 736 x 414
} else if (screenSize.height == 736 || screenSize.width == 736 ) {
device = LFDeviceTypePhone6Plus;
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
device = LFDeviceTypePadClassic; // Just in case it doesn't make it through the conditionals
if(deviceScale == 1.0f) {
device = LFDeviceTypePadClassic;
} else if (deviceScale == 2.0f) {
device = LFDeviceTypePadRetina;
}
}
//NSLog(#"The device is %# scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width);
return device;
}
Call it like this:
if ( ( [Utilities deviceType] == LFDeviceTypePhoneClassic
|| [Utilities deviceType] == LFDeviceTypePhoneRetina3_5) &&
numberOfFoilsOnScreen > 7 ) {
numberOfFoilsOnScreen = 7;
}
Related
I have button called login. when i see the button font size it looks same in all the devices, though the button width and height vary. How to define different font size for different devices?. I am talking about only for iPhone portrait. So don't give solution as size class.
You can use font size variation - define size to font in Storyboard, look in below image it shows how to define size to font.
Click on small + button besides Font property, a pop up will appear.
As shown in above image you can define size for Width and Height for different variation.
Try this
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
mybutton.titleLabel.fontt = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
mybutton.titleLabel.font = mybutton.titleLabel.font.fontWithSize(20)
}
You can check the iPhone device size and then in if-else loop apply your button font size logic.
#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 7 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 10 :61 )))))))
if (iPhoneVersion == 4)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:12];
}
else if (iPhoneVersion == 5)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:14];
}
else if (iPhoneVersion == 6)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:16];
}
else if (iPhoneVersion == 7)
{
mybutton.titleLabel.font = [UIFont systemFontOfSize:17];
}
and same goes for all devices.
I've read all the questions about this issue, all the web articles but still can't get it the right way.
I designed the app for iPhone 6+ (2208x1242), now I the PSD that I want to cut. For what size I need to resize in order to cut it the right size for iPhone 6/5/5S?
Just can't figure it out, so please help me..
Thanks in advance.
Here is a link with the resolution for all the other iPhone devices:
http://iphoneresolution.com/
You should just resize the background and cut the button and all other UI elements, if the elements need to be resized take care in resizing them by maintaining the aspect ratio.
Cheers,
I hope it helped you
This might help. Reported screen size is in the code. Actual screen size is in comments.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
device = #"iPhone Classic"; // Just in case it doesn't make it through the conditionals
// Classic has a resolution of 480 × 320
if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f ) {
device = #"iPhone Classic";
// Retina has a resolution of 960 × 640
} else if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f ) {
device = #"iPhone Retina35";
// Retina 4" has a resolution of 1136 x 640
} else if (screenSize.height == 568 || screenSize.width == 568 ) {
device = #"iPhone Retina4";
// iPhone 6 has a resolution of 1334 by 750
} else if (screenSize.height == 667 || screenSize.width == 667 ) {
device = #"iPhone 6";
// iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080
// Reported size is 736 x 414
} else if (screenSize.height == 736 || screenSize.width == 736 ) {
device = #"iPhone 6 Plus";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
device = #"iPad Classic"; // Just in case it doesn't make it through the conditionals
if(deviceScale == 1.0f) {
device = #"iPad Classic";
} else if (deviceScale == 2.0f) {
device = #"iPad Retina";
}
}
does anyone know an alternative to the following code? I want to support iOS 7 and below in my app and .nativeScale does not work on these firmwares. I could not find a solution on the internet that's why I am asking here. (normally letting screenbounds check for height of 568 does not work for iPhone 6+)
The Code I'm referring to:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if ([UIScreen mainScreen].nativeScale > 2.1) {
//6 plus
} else if (screenBounds.size.height == 568) {
//4 inch / iPhone 6 code
} else {
//3.5 inch code
}
Thanks in advance
So what I did was:
First I had the methods in the following order:
if ([UIScreen mainScreen].nativeScale > 2.1) {
//6 plus
} else if (screenBounds.size.height == 568) {
//4 inch code
} else {
//3.5 inch code
}
Then I thought since the computer stops running the if else statement once he finds a true one I just rearranged the order to the following:
if (screenBounds.size.height == 480) {
//3.5 inch code
} else if ([UIScreen mainScreen].nativeScale > 2.1) {
//6 plus
} else if (screenBounds.size.height == 568) {
//4 inch code
}
This supported an iPhone 4S with iOS 7 or below. On iPhone 5 / 5S it would still crash. That's why I changed it in the end to the following:
if ([[UIScreen mainScreen] respondsToSelector:#selector(nativeScale)]) {
//checks if device is running on iOS 8, skips if not
NSLog(#"iOS 8 device");
if (screenBounds.size.height == 480) {
//3.5 inch code
NSLog(#"iPhone 4S detected");
} else if ([UIScreen mainScreen].nativeScale > 2.1) {
//6 plus
NSLog(#"iPhone 6 plus detected");
} else if (screenBounds.size.height == 568) {
//4 inch code
NSLog(#"iPhone 5 / 5S / 6 detected");
}
} else if (screenBounds.size.height == 480) {
//checks if device is tunning iOS 7 or below if not iOS 8
NSLog(#"iOS 7- device");
NSLog(#"iPhone 4S detected");
//3.5 inch code
} else if (screenBounds.size.height == 568) {
NSLog(#"iOS 7- device");
NSLog(#"iPhone 5 / 5S / 6 detected");
//4 inch code
}
It should now fully work on any iOS 7 an iOS 8 device!
Forgive me if this is a really stupid question I am just a beginner at all of this stuff. I want to make a separate interface for the iPhone 6 and 6 Plus but I don't know how the best way to be going about this is. This is a very simple app just to get myself more familiar with the language, etc. I was wondering if there is an easy way to tell if a user is using a 6 or 6 plus to make a separate interface for them. Thanks :)
Use single storyboard. Learn autolayout from raywanderlich, it think its a very good resource.
Also,now take advantage of the size classes introduced for ios 8 in xcode 6. This is called adaptive layout.
So instead of making the above four views for 3.5, 4, 4.7, 5.5 inch devices separately, and then worry about combining their respective landscape modes as well, just go for the 4 combination of size classes using auto layout (if possible, highly recommended) and relax for any other device size that may come into future.
To detect iPhone Version Please use below code.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 667){
// iPhone retina-4.7 inch(iPhone 6)
}
else if([UIScreen mainScreen].bounds.size.height == 568){
// iPhone retina-4 inch(iPhone 5 or 5s)
}
else{
// iPhone retina-3.5 inch(iPhone 4s)
}
}
else if ([[UIScreen mainScreen] scale] == 3.0)
{
if([UIScreen mainScreen].bounds.size.height == 736.0){
//iPhone retina-5.5 inch screen(iPhone 6 plus)
}
}
}
Reference from Here
write this code in .pch file
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
and wwhenever you need in any controller you simply check the condition.
Below are methods that do what you asked. One major thing to look out for is in iOS7 and below, when you check [[UIScreen mainScreen] bounds] for the screen bounds, it always lists the width and height as the same no matter what orientation the phone is in. So if it's an iPhone5 in landscape mode, it will still list the width as 320 and height as 568. In iOS8, this changed, now if that same iPhone5 is in landscape, it will list the width as 568 and the height as 320. Below are methods which account for this:
+ (BOOL) deviceHasFourInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0];
}
+ (BOOL) deviceHasFourPointSevenInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0];
}
+ (BOOL) deviceHasFivePointFiveInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0];
}
+ (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height
{
CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
CGFloat mainScreenHeight;
if ([OperatingSystemVersion operatingSystemVersionLessThan:#"8.0"])
{
mainScreenHeight = mainScreenBounds.size.height;
}
else
{
mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height)
{
return YES;
}
else
{
return NO;
}
}
Also here are the accompanying OperatingSystem class methods:
+ (NSString *) currentOperatingSystemVersion
{
return [[UIDevice currentDevice] systemVersion];
}
+ (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare
{
return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);
}
Put below lines in prefix.pch
#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : 999))))
Now in programming you can say...
if (iPhoneVersion==4) {
NSLog("This is 3.5 inch iPhone - iPhone 4s or below");
} else if (iPhoneVersion==5) {
NSLog("This is 4 inch iPhone - iPhone 5 family");
} else if (iPhoneVersion==6) {
NSLog("This is 4.7 inch iPhone - iPhone 6");
} else if (iPhoneVersion==61) {
NSLog("This is 5.5 inch iPhone - iPhone 6 Plus.. The BIGGER");
} else {
NSLog("This is iPad");
}
However, I would say use one storyboard for all iPhones by learning autolayout.
This question already has answers here:
How to detect iPhone 5 (widescreen devices)?
(24 answers)
Closed 9 years ago.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
CGSize result = [[UIScreen mainScreen] bounds].size;
//NSLog(#"phonesize%#",result);
if(result.height > 500){
NSLog(#"iPhone 5");
}
else{
NSLog(#"iPhone 4");
}
}
My Mac OS is 10.7.2 and Xcode 4.5.2.I used above code to find device is iPhone 4 or iPhone 5
It is giving iPhone4 when i uses iPhone5.I did set launch image and icon image. Is there anything else i missed out?
Make sure you have a PNG file named Default-568h#2x.png in your project.
You need to scale the result you got as per the screen's scale scale and then check ...
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");
}
if(result.height == 1136) {
NSLog(#"iPhone 5");
}
}
else{
NSLog(#"Standard Resolution");
}
}
Try this code instead its tested.
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone)
{
//device is iPhone
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
//device is iphone 5
//add storyboard/Xib that have large screen (320*568)
} else {
//device is iphone 4
//add story board/Xib that have small screen (320*480)
}
}
else
{
//device is iPad
}
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
//iphone 5
else
{
//iphone 4
}
}
else
{
//iPad
}
Try this one might be helpful to you...