This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
IPhone/IPad: How to get screen width programmatically?
How to get orientation-dependent height and width of the screen?
Accounting for the various iOS devices and orientations, what is the simplest approach to getting the current screen resolution, including height and width?
UIScreen is your friend here.
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
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.
Hope it helps you..
Related
I put this code in my app:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
NSLog(#"screenWidth : %f", screenWidth);
NSLog(#"screenHeight : %f", screenHeight);
The I start the simulator with Iphone 3.5 inch.
The log prints 320.000000 for the width.
However, when I then change the device for the simulator, selecing 4inch, it stills shows the same width. Why? Whats wrong. It is supposed to be 640 px in width for Iphone 4 and above, doesnt it?
In the simulator screenWidth will be always 320 ( right now iphone4-5 ). You can check the scale with [[UIScreen mainScreen] scale];
scale will be 2.0 if you running on retina iPhone.
According to the Apple Docs, that value is the "default logical value". If you want to know the actual size, take a look at [[UIScreen mainScreen] scale]. For retina devices, this value is 2.0, so do [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale]
no the width is always 320 POINTS / DPIs -- what you mean is that it really has 640 PIXELS (which are device dependant)
Calculations are always based on device INDEPENDENT points
UIScreen offers a scalingFactor that translates dpi <> pixels
I try to make my Views completely dynamic so the Views can fit any Screen Dimensions. At the Moment i get the screen Width and Height for calculation like this:
CGFloat getDisplayHeight() {
CGSize result = [[UIScreen mainScreen] bounds].size;
return result.height;
}
CGFloat getDisplayWidth() {
CGSize result = [[UIScreen mainScreen] bounds].size;
return result.width;
}
The Problem is that the "Resizable Iphone" Emulator always give me a Width of 768 and a Height of 1024 (Like the iPad). It doesn´t matter when i change the Width and Height Values on the Bottom of the Emulator...is there any new Function i can use for better testing?
EDIT:
I´m still not sure how to handle this exactly...At the Moment i calculate the Size of my Subviews in "viewDidLayoutSubviews" because this is the only Method which shows the correct Width and Heigth of the "resizable Iphone Simulator", the Problem is that when i calculate all my Stuff inside this method it gets called every time when i scroll or touch the UI, so it could be the case that it gets called hundreds of times...and when i try to calculate my Views in "ViewDidAppear" its too late and evrything look reallys weird after the recalculation....
I want to get the width and height of the screen, with this code ...
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float screenHeight = [UIScreen mainScreen].bounds.size.height;
NSLog(#"%f %f",screenWidth, screenHeight);
I get always 320 480 , but these aren't the true measures of the simulator. Now I'm testing on a retina simulator with more resolution, but i always get 320 480
I think your answer can be found here: in iPhone App How to detect the screen resolution of the device
Its the scale that interests you as well. Use CGFloat screenScale = [[UIScreen mainScreen] scale]; to find out the scale.
When positioning elements your points should always be in non-retina scale. Size of the screen for 3.5 inch is 320x480 units and 320x568 for the 4 inch screen. The pixel resolution is times two for retina screens.
What do I need to do to run in full resolution? No matter what I do,
UIScreen.MainContent.Bounds => {1024x768}
I am running Monodevelop 2.8.8.4 and I am setting my Deployment Target to 5.1
Any Ideas?
Thanks
Can't verify this since I don't have an iPad3, but on iPhone4's retina display bounds does not tell the whole story.
You need to take scale into consideration too;
CGRect bounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize realSize =
CGSizeMake(bounds.size.width * scale, bounds.size.height * scale);
That would most likely translate to multiplying width and height by UIScreen.MainScreen.Scale on MonoTouch.
Why do I see in some sample code from Apple (such as PhotoScroller) that I should do the following in loadView:
CGRect frame = [[UIScreen mainScreen] bounds];
instead of
CGRect frame = [[UIScreen mainScreen] applicationFrame];
Does it make a difference to get the main screen's frame?
ApplicationFrame is the screen size minus the size of the status bar (if visible), bounds is the screen size regardless of status bar.
So applicationFrame would return CGRectMake(0,0,320,460) assuming your app has the status bar set to be visible, while bounds would return CGRectMake(0,0,320,480) under the same conditions. Those numbers are assuming iPhone/iPod Touch screen sizes.
UIScreen Class Reference
iOS 9 Update
When using the Split View feature on the iPad, applicationFrame returns the size of your app window. bounds always returns the size of the device.