iPhone 4 incorrect screen size? - iphone

Could someone tell me why the following code would return Screen Size: 320.000000, 480.000000 on the iPhone 4?
CGRect screenRect = [[UIScreen mainScreen] bounds];
NSLog(#"Screen Size: %f, %f", screenRect.size.width, screenRect.size.height);

That method returns a size in Points, not Pixels. If you are a registered apple developer, I would suggest watching the WWDC (new) video on designing for the Retina display. It has a load of really useful information.
UIKit uses points, however OpenGL uses pixels. UIViews now have a contentScaleFactor property, which will be 2 on iPhone 4, and 1 on everything else so far...

Related

UIWebView pdf content is no resized in iPhone 5 (4-inch)

I'm using UIWebView to load local 5 pages pdf file, the page content is perfect in iPhone (3.5 inch), but it's not resized appropriately in 4-inch, check the screen shot.
How can I fix it?
Thanks
I dont think you can fix it. UIWebView scales down pdf's to make them fit the iPhone screen, but it wont stretch them.
If the pdf is local as you say, you could always include 2 pdf files, one for iPhone 4inch and one for 3.5inch.
you can check the screen size for 4inch or 3.5inch
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
//for iphone 5
//for 4 inch
}
else
{
//for iphone 4
//for 3.5 inch
}

converting iPhone App to iPad

I'm starting to convert iPhone App to iPad, the app is a special calculator and has a feel and look like one. on iPhone is to use portrait since it the best fit.
My questions are:
i. when I run it on the iPad do i need to use the whole iPad screen size? I would like just to upscale all my controls size and width by let's say 125% of the original size. How do I check the device screen size and how do I center my calculator to be in the center when running on iPad? currently the iPad is CGRectMake(0,0,640,1024) but it may change in the future.
ii. I also like it to adjust accordingly on both orientations of the iPad but only portrait on the iPhone. and I'm not sure how to do it in the most effective-easy way... if you may advice.
my current iPhone code has .xib with the different controls but I adjust them all using the
-setFrame:CGRectMake(...) in code, so far I set the TARGETED_DEVICE_FAMILY in project settings to run app on both devices but didn't change any of the -setFrame:CGRectMake(...) to be dynamic depending on the device.
TIA
1) You can ask [UIScreen mainScreen] for its bounds - that gives you the size.
2) You need to respond to the rotation methods in your UIViewController related to rotation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
3) You can use this to find out if your app is running on an iPhone or iPad:
[[UIDevice currentDevice] userInterfaceIdiom]
4) To center the view in a larger view use this code:
+ (CGRect)centeredFrameForSize:(CGSize)size inRect:(CGRect)rect
{
CGRect frame;
frame.origin.x = rintf((rect.size.width - size.width)/2) + rect.origin.x;
frame.origin.y = rintf((rect.size.height - size.height)/2) + rect.origin.y;
frame.size = size;
return frame;
}

Problem adapting scale factor for iPad x2 compatibility mode

I wonder if anyone can help me with following. I have written a Quartz 2d ap and have used the following code to get the correct scale factor for each device :
if ([UIScreen instancesRespondToSelector:#selector(scale)])
{
return [[UIScreen mainScreen] scale];
}
else
{
return 1.0;
}
I then multiply all values by this scale mulitplier in my code. The problem I have is that the app does not display propertly in x2 mode on the ipad - everything is two times too big. Can anybody help me ?
Thanks,
Martin
The scale factor is related to the Retina displays on the newer iPhones and iPod touches, not the 2X scaling setting on the iPad. In fact, the UIScreen scale property you are referencing does not exist on the iPad's current 3.2 OS version, only on 4.0+. On the current iPads running the OS 4.2 beta, it should always return 1.0.
The problem you are experiencing with Quartz drawing in the 2X mode must come from somewhere else. Do you do any device-specific checks for any elements in your code?
I am not sure if this is your problem, but you seem to want to test the UIScreen for the selector scale. Which it will never have. That selector works only on [UIScreen mainScreen].
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
{
return [[UIScreen mainScreen] scale];
}
else
{
return 1.0;
}
Although, this mistake would let you think that it would always return a scale of 1.0.

Is there a safe way to find the display size in pixels in universal app?

I get some strange problems when trying to get the screen size in my universal iPhone/iPad app.
I was first using
[[UIScreen mainScreen] bounds]
But it does not return the correct size for iPhone 4 (at least not in the simulator), it just returns 320x480 for all iPhones
Then I changed to
CGSize screenSize = mainscr.currentMode.size;
And it works in the simulator for all apple devices, but when running this line on an iPhone 3GS device the program exits with a SIGABRT
Device is running 3.1.2
Any idea how to get the pixel dimension of the display in a device safe way?
UIScreen.currentMode is not available in < 3.2, so you need to check with -respondsToSelector:
CGSize screenSize;
if ([mainscr respondsToSelector:#selector(currentMode)])
screenSize = mainscr.currentMode.size;
else
screenSize = mainscr.bounds.size;
Similarly, UIScreen.scale is not available in < 4.0, if you use that, check with -respondsToSelector:
.
CGFloat scale = [mainscr respondsToSelector:#selector(scale)] ? mainscr.scale : 1.0f;
[[UIScreen mainScreen] bounds] returns a value in points not in pixels but you can use the scale parameter to convert the resolution in pixels.
The proper way to think about is that the screen resolution IS 320x480 but with a display scale of 2.0. Realize that it is very likely that 'other' apple devices in the future will have other display scales.. imagine for example a new iPad someday that has a scale of 1.5...
if([[UIScreen mainScreen] respondsToSelector:#selector"scale"]) {
displayScale = [[UIScreen mainScreen] scale]; }
The reason they did this is to make it easy to write apps that work on any device. You can put an object on the screen at 100,100 and it will be in the same place on both devices. Use the #2x naming method to provide two sets of images, one at 1x scale and one at 2x scale.

Avoid hardcoding iPhone screen size with programmatic view creation

I was looking up how to create a view programmatically and found the following example code:
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
This works great, except I don't like that it hardcodes the size of the screen. Is there a way to look up the size of the screen? An important point someone brought up is that if the app is running during a phone call then the screen will be slightly smaller because of the green "return to call" bar.
Is there a way to look up the size of
the screen?
Yup:
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
applicationFrame
This property
contains the screen bounds minus the
area occupied by the status bar, if it
is visible. Using this property is the
recommended way to retrieve your
application’s initial window size. The
rectangle is specified in points.
[[UIScreen mainScreen] bounds] or
[[UIScreen mainScreen] applicationFrame]
should be all you need to get the full screen size.
I've only used the applicationFrame version myself.
(I also suggested [UIHardware fullScreenApplicationContentRect] but that was from some very old code - don't use it)