I'm trying to programmatically determine the current height and width of my application. I use this:
CGRect screenRect = [[UIScreen mainScreen] bounds];
But this yields a width of 320 and a height of 480, regardless of whether the device is in portrait or landscape orientation. How can I determine the current width and height (i.e. dependent upon the device orientation) of my main screen?
You can use something like UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) to determine the orientation and then use the dimensions accordingly.
HOWEVER, during an orientation change like in UIViewController's
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
Use the orientation passed in toInterfaceOrientation since the UIApplication's statusBarOrientation will still point to the old orientation as it has not yet changed (since you're inside a will event handler).
Summary
There are several related posts to this, but each of them seem to indicate that you have to:
Look at [[UIScreen mainScreen] bounds] to get the dimensions,
Check what orientation you are in, and
Account for the status bar height (if shown)
Links
Iphone: Get current view dimensions or screen dimensions
IPhone/IPad: How to get screen width programmatically?
Objective C - how to get current screen resolution?
“Incorrect” frame / window size after re-orientation in iPhone or iPad
iPhone Dev SDK - get screen width
Working Code
I usually don't go this far, but you piqued my interest. The following code should do the trick. I wrote a Category on UIApplication. I added class methods for getting the currentSize or the size in a given orientation, which is what you would call in UIViewController's willRotateToInterfaceOrientation:duration:.
#interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
#end
#implementation UIApplication (AppDimensions)
+(CGSize) currentSize
{
return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
CGSize size = [UIScreen mainScreen].bounds.size;
UIApplication *application = [UIApplication sharedApplication];
if (UIInterfaceOrientationIsLandscape(orientation))
{
size = CGSizeMake(size.height, size.width);
}
if (application.statusBarHidden == NO)
{
size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
}
return size;
}
#end
To use the code simple call [UIApplication currentSize]. Also, I ran the above code, so I know it works and reports back the correct responses in all orientations. Note that I factor in the status bar. Interestingly I had to subtract the MIN of the status bar's height and width.
Other thoughts
You could go about getting the dimensions by looking at the UIWindow's rootViewController property. I've looked at this in the past and it similarly reports the same dimensions in both portrait and landscape except it reports having a rotate transform:
(gdb) po [[[[UIApplication sharedApplication] keyWindow]
rootViewController] view]
<UILayoutContainerView: 0xf7296f0; frame =
(0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H;
layer = <CALayer: 0xf729b80>>
(gdb) po [[[[UIApplication
sharedApplication] keyWindow] rootViewController] view]
<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); autoresize
= W+H; layer = <CALayer: 0xf729b80>>
Not sure how your app works, but if you aren't using a navigation controller of some kind, you could have a UIView under your main view with the max height / width of parent and grows / shrinks with parent. Then you could do: [[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]. That looks pretty intense on one line, but you get the idea.
However, it would still be better to do the above 3 steps under the summary. Start messing with UIWindows and you'll find out weird stuff, like showing a UIAlertView will change UIApplication's keywindow to point at a new UIWindow that the UIAlertView created. Who knew? I did after finding a bug relying on keyWindow and discovering that it changed like that!
This is my solution code !This method can add to NSObject class's Categroy , or you can define a Top custom UIViewController class , and let all of your other UIViewControllers to inherit it .
-(CGRect)currentScreenBoundsDependOnOrientation
{
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
}
return screenBounds ;
}
Note, after IOS8 , as Apple Document of UIScreen's bounds property says :
Discussion
This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.
so for the consideration of compatibility , we should detect the IOS version and make the change as below:
#define IsIOS8 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
-(CGRect)currentScreenBoundsDependOnOrientation
{
CGRect screenBounds = [UIScreen mainScreen].bounds ;
if(IsIOS8){
return screenBounds ;
}
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
}
return screenBounds ;
}
Here's a handy macro:
#define SCREEN_WIDTH (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
In iOS 8+ you should use the viewWillTransitionToSize:withTransitionCoordinator method:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
// You can store size in an instance variable for later
currentSize = size;
// This is basically an animation block
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Get the new orientation if you want
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// Adjust your views
[self.myView setFrame:CGRectMake(0, 0, size.width, size.height)];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Anything else you need to do at the end
}];
}
This replaces the deprecated animation method that gave no information about size:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
As of iOS 8 screen bounds are now returned correct for current orientation. This means an iPad in landscape orientation [UIScreen mainScreen].bounds would return 768 on iOS <=7 and 1024 on iOS 8.
The following returns the correct height and width on all versions released.
-(CGRect)currentScreenBoundsDependOnOrientation
{
NSString *reqSysVer = #"8.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
return [UIScreen mainScreen].bounds;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
NSLog(#"Portrait Height: %f", screenBounds.size.height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
NSLog(#"Landscape Height: %f", screenBounds.size.height);
}
return screenBounds ;
}
if you want the orientation dependent size and you have a view, you can just use:
view.bounds.size
I wrote category for UIScreen, that works on all iOS versions, so you can use it like this:
[[UIScreen mainScreen] currentScreenSize].
#implementation UIScreen (ScreenSize)
- (CGSize)currentScreenSize {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBounds.size;
if ( NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if ( UIInterfaceOrientationIsLandscape(interfaceOrientation) ) {
screenSize = CGSizeMake(screenSize.height, screenSize.width);
}
}
return screenSize;
}
#end
Here is a Swift way to get orientation dependent screen sizes:
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
These are included as a standard function in a project of mine:
https://github.com/goktugyil/EZSwiftExtensions
float msWidth = [[UIScreen mainScreen] bounds].size.width*(IS_RETINA?2.0f:1.0f);
float msHeight = [[UIScreen mainScreen] bounds].size.height*(IS_RETINA?2.0f:1.0f);
if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
os->setWidth(MIN(msWidth, msHeight));
os->setHeight(MAX(msWidth, msHeight));
} else {
os->setWidth(MAX(msWidth, msHeight));
os->setHeight(MIN(msWidth, msHeight));
}
NSLog(#"screen_w %f", os->getWidth());
NSLog(#"screen_h %f", os->getHeight());
However, on iOS 8.0.2:
+ (NSUInteger)currentWindowWidth
{
NSInteger width = 0;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGSize size = [UIScreen mainScreen].bounds.size;
// if (UIInterfaceOrientationIsLandscape(orientation)) {
// width = size.height;
// } else {
width = size.width;
// }
return width;
}
use -> setNeedsDisplay() for the view you want to resize.
Some improvements on the answers offered here, in Swift:
let interfaceOrientation = UIApplication.shared.statusBarOrientation // (< iOS 13)
let screenSize = UIScreen.main.bounds.size
let screenWidth = interfaceOrientation.isPortrait ? screenSize.width : screenSize.height
let screenHeight = interfaceOrientation.isPortrait ? screenSize.height : screenSize.width
Related
Was trying to change several labels and images width depend on the screen bounds after device gets rotated. Here's what I've tried:
- (void)viewDidLayoutSubviews{
if( UIInterfaceOrientationIsLandscape( [[UIDevice currentDevice] orientation] )) {
CGSize newSize;
newSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
NSLog(#"%#",NSStringFromCGSize(newSize));
}else if (UIInterfaceOrientationIsPortrait( [[UIDevice currentDevice] orientation] )){
CGSize newSize;
newSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height);
NSLog(#"%#",NSStringFromCGSize(newSize));
}
}
It's supposed to log different width and height when I change the orientation
(like on iphone5 : {480,320} & {320,480})
works fine on ios8,
but I got the same size results testing both on ios7 simulator and ios7 device.
Any idea would be appreciated.
Only from iOS8
[UIScreen mainScreen].bounds
is interface-oriented (look at session "View Controller Advancements in iOS 8" of WWDC 2014), this means that in landscape mode on iOS8 height and width are reversed.
You can create an helper method like below to have the same behavior:
+(CGRect)getScreenBounds
{
CGRect bounds = [UIScreen mainScreen].bounds;
if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
}
return bounds;
}
I'm getting in a bit of a muddle trying to draw some paintcode graphics code in the middle of the screen horizontally.
I'm really not sure if scale or landscape orientation affects this.
I've created a UIView subclass and addSubview in viewDidLoad like so...
MenuTitleView *fancyView = [[MenuTitleView alloc] initWithFrame:self.view.bounds];
[[self view] addSubview:fancyView];
Rather than the size PaintCode gave me...
CGRect textRect = CGRectMake(0, 0, 418, 129);
I'm trying to determine the screen / view width and the size of the canvas width.
I've tried various things without success.
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat w = screenRect.size.width;
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = (w - width) * 0.5f;
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
Scale is not an issue, because Quartz and UIKit now work in 'points' rather than in pixels, so that's already accounted for. However orientation is not, [UIScreen mainScreen] will return the same rect either for applicationFrame or bounds etc, it's oblivious to orientation.
I like to put a solution in a category on UIView or UIViewController, because you'll reuse it plenty. something like this...
-(CGRect )workingCanvas{
CGRect screenRect = [UIScreen mainScreen].bounds;
BOOL landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
CGRect result = CGRectZero;
CGFloat lesserDimension = (screenRect.size.width < screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
CGFloat greaterDimension = (screenRect.size.width > screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
result.size.width = (landscape) ? greaterDimension : lesserDimension;
result.size.height = (landscape) ? lesserDimension : greaterDimension;
if ([[UIApplication sharedApplication]isStatusBarVisible]){
result.size.height -= [[UIApplication sharedApplication]statusBarFrame].size.height;
}
return result;
}
if you want to position your view just in the middle of the screen try this
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = CGRectGetMidX(screenRect);
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
already tested is working
Good Luck!!
I have used this method for detecting the width and height of screen. But its showing the width as 768 and height as 1024 in portrait and landscape also.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
float widthfloat= screenBounds.size.width;
float heightfloat= screenBounds.size.height;
NSLog(#" width float %f",widthfloat);
NSLog(#"height float %f",heightfloat);
NSLog(#"width view %f \n height view %f", self.view.bounds.size.width, self.view.bounds.size.height);
NSLog(#"width %f \n height %f", screenBounds.size.width, screenBounds.size.height);
float wvalue = [[UIScreen mainScreen] bounds].size.width;
float hvalue = [[UIScreen mainScreen] bounds].size.height;
NSLog(#" wvalue %f",wvalue);
NSLog(#"hvalue %f",hvalue);
CGFloat width1 = [UIScreen mainScreen].bounds.size.width;
CGFloat height1 = [UIScreen mainScreen].bounds.size.height;
NSLog(#"width1 %f",width1);
NSLog(#"height1 %f",height1);
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGRect screenBounds1 = [[UIScreen mainScreen] bounds];
CGSize screenSize = CGSizeMake(screenBounds1.size.width * screenScale, screenBounds1.size.height * screenScale); if (screenSize.height==1136.000000)
NSLog(#"abcd1 %f",screenBounds1.size.width);
NSLog(#"abcd2 %f",screenBounds1.size.height);
NSLog(#"efgh1 %f",screenSize.width);
NSLog(#"efgh2 %f",screenSize.height);
bounds contains the bounding rectangle of the screen, measured in points. It does not care about screen orientation.
You can calculate the screen size, I do something like this for me -
-(CGSize)screenSize
{
CGSize size = [UIScreen mainScreen].applicationFrame.size;
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
return size;
else
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
if (size.height > 480.0)
// This is iPhone-5
size = CGSizeMake(568.0, 320.0);
else
// This is iPhone-4
size = CGSizeMake(480.0, 320.0);
}
else
{
// This is iPad
size = CGSizeMake(1024.0, 768.0);
}
if (![[UIApplication sharedApplication] isStatusBarHidden])
size.height -= 20.0;
return size;
}
}
Note: The method will give you the height after reducing the height of the statusbar if available.
I'm using the following code to get the screen size width:
CGFloat width = [UIScreen mainScreen].bounds.size.width - 100;
But its giving width as "668.0" in portrait as well as landscape.
How can I get different width depending on the orientation of the device.
I ran into the same problem as you and all I could find is checking the orientation and calculate the height and width as follow:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
screenHeight = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 748 : 1004;
screenWidth = orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight ? 1024 : 768;
These size are for the the iPad obvioulsy but it could work for iPhone with the right dimensions.
Personally I think it's a bit poor but I couldn't find anything else.
Ok. In that case we can use,
CGRect viewFrame = self.view.frame;
int width = viewFrame.size.width;
int height = viewFrame.size.height;
CGSize winSize = [[CCDirector sharedDirector] winSize];
winSize.width will give you width and winSize.height give you height.
In iPhone App,
while running the App on device How to detect the screen resolution of the device on which App is running?
CGRect screenBounds = [[UIScreen mainScreen] bounds];
That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.
CGFloat screenScale = [[UIScreen mainScreen] scale];
This will give you the scale of the screen. For all devices that do not have Retina Displays this will return a 1.0f, while Retina Display devices will give a 2.0f and the iPhone 6 Plus (Retina HD) will give a 3.0f.
Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
By multiplying by the screen's scale you get the actual pixel resolution.
A good read on the difference between points and pixels in iOS can be read here.
EDIT: (Version for Swift)
let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
UIScreen class lets you find screen resolution in Points and Pixels.
Screen resolutions is measured in Points or Pixels. It should never be confused with screen size. A smaller screen size can have higher resolution.
UIScreen's 'bounds.width' return rectangular size in Points
UIScreen's 'nativeBounds.width' return rectangular size in Pixels.This value is detected as PPI ( Point per inch ). Shows the sharpness & clarity of the Image on a device.
You can use UIScreen class to detect all these values.
Swift3
// Normal Screen Bounds - Detect Screen size in Points.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
print("\n width:\(width) \n height:\(height)")
// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.main.nativeBounds.width
let nHeight = UIScreen.main.nativeBounds.height
print("\n Native Width:\(nWidth) \n Native Height:\(nHeight)")
Console
width:736.0
height:414.0
Native Width:1080.0
Native Height:1920.0
Swift 2.x
//Normal Bounds - Detect Screen size in Points.
let width = UIScreen.mainScreen.bounds.width
let height = UIScreen.mainScreen.bounds.height
// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.mainScreen.nativeBounds.width
let nHeight = UIScreen.mainScreen.nativeBounds.height
ObjectiveC
// Normal Bounds - Detect Screen size in Points.
CGFloat *width = [UIScreen mainScreen].bounds.size.width;
CGFloat *height = [UIScreen mainScreen].bounds.size.height;
// Native Bounds - Detect Screen size in Pixels.
CGFloat *width = [UIScreen mainScreen].nativeBounds.size.width
CGFloat *height = [UIScreen mainScreen].nativeBounds.size.width
Use it in App Delegate: I am using storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
//----------------HERE WE SETUP FOR IPHONE 4/4s/iPod----------------------
if(iOSDeviceScreenSize.height == 480){
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:#"iPhone" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
iphone=#"4";
NSLog(#"iPhone 4: %f", iOSDeviceScreenSize.height);
}
//----------------HERE WE SETUP FOR IPHONE 5----------------------
if(iOSDeviceScreenSize.height == 568){
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:#"iPhone5" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
NSLog(#"iPhone 5: %f", iOSDeviceScreenSize.height);
iphone=#"5";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// NSLog(#"wqweqe");
storyboard = [UIStoryboard storyboardWithName:#"iPad" bundle:nil];
}
return YES;
}
For iOS 8 we can just use this [UIScreen mainScreen].nativeBounds , like that:
- (NSInteger)resolutionX
{
return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}
- (NSInteger)resolutionY
{
return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}
See the UIScreen Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(#"scale")])
{
if ([[UIScreen mainScreen] scale] < 1.1)
NSLog(#"Standard Resolution Device");
if ([[UIScreen mainScreen] scale] > 1.9)
NSLog(#"High Resolution Device");
}
Use this code it will help for getting any type of device's screen resolution
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
If your goal is to get the model resolution type and not the resolution values themeselfs, this Swift solution might be helpful:
import UIKit
#objc(IphoneModelScreenSize)
public class IphoneModelScreenSize: NSObject {
// MARK: Enums
public enum IphoneModelScreenSize: Int {
case notAnIphone = 0,
twoThreeOrFour = 1,
se = 2,
sixSevenOrEight = 3,
plus = 4,
elevenXorXS = 5,
elevenProMaxOrXsMax = 6
}
// MARK: Class properties
public class func screenSize() -> IphoneModelScreenSize {
let bounds = UIScreen.main.bounds
let screenWidth = bounds.size.width
let screenHeight = bounds.size.height
switch (screenWidth, screenHeight) {
case (320.0, 480.0):
return .twoThreeOrFour
case (320.0, 568.0):
return .se
case (375.0, 667.0):
return .sixSevenOrEight
case (414.0, 736.0):
return .plus
case (375.0, 812.0):
return .elevenXorXS
case (414.0, 896.0):
return .elevenProMaxOrXsMax
default:
return .notAnIphone
}
}
public class func screenSizeStringValue() -> String {
return screenSizeEnumToString(screenSize())
}
// MARK: Private properties
private class func screenSizeEnumToString(_ screenSize: IphoneModelScreenSize) -> String {
var screenSizeAsString: String
switch screenSize {
case .notAnIphone:
screenSizeAsString = "Not an Iphone"
case .twoThreeOrFour:
screenSizeAsString = "2G, 3G, 3GS, 4 or 4s"
case .se:
screenSizeAsString = "5, 5s, 5c or SE"
case .sixSevenOrEight:
screenSizeAsString = "6, 6s, 7 or 8"
case .plus:
screenSizeAsString = "6+, 6s+, 7+ or 8+"
case .elevenXorXS:
screenSizeAsString = "11 Pro, X or Xs"
case .elevenProMaxOrXsMax:
screenSizeAsString = "11, Xr, 11 Pro Max or Xs Max"
}
return screenSizeAsString
}
}