Avoid hardcoding iPhone screen size with programmatic view creation - iphone

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)

Related

iPhone 5 UITableView

I am trying to update my App for the iPhone 5's larger screen real estate. I have a tableview that has expanded to take up the full height of the screen by adding the Default-568h#2x.png to my project, but the bottom couple of cells are not responding to touched in the simulator or the actual iPhone 5. Am I missing something? It's like the bottom portion of the screen is not detecting touched (but just in my App not the others).
Any thoughts?
Thanks,
Rob
This is whats working for me
In application didFinishLaunching method I added this -
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
In any other view controller, preferably in - (void)viewDidLoad;
UIScreen *screen = [UIScreen mainScreen];
[self.view setFrame:[screen applicationFrame]];
Yes may be your window frame size in AppDel class or in XIB file of your project's appdel class is not as per iPhone 5 Screen.
And change window frame according 3.5 inch and 4 inch screen of iPhone

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.

iPhone 4 incorrect screen size?

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...

How to set brigtness of ipad or iphone by programming?

I have implementing pdf application for ipad.Now i want to set brightness in my application.Is it possible?Please help me for this problem.
Thanks in advance.
Most applications that you see with this feature are putting up a full screen solid black view or window and adjusting the alpha level. The brightness is then adjusted from some minimum value to the current system maximum. The actual system brightness cannot be accessed by a sandboxed application.
UIWindow *shader = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds];
shader.backgroundColor = [UIColor blackColor];
shader.alpha = 0.1;
With a view, you have the option of being behind things like toolbars. With a window, you can be over everything if you set the window level high enough. Do not let the alpha level go above 0.5 or whatever you choose as your limit.
short answer: you can't in the official sdk.

ABPeoplePickerNavigationController

This is probably a really basic question, but when I make my ABPeoplePickerNavigationController instance (addBookViewer)visible by running,
[self presentModalViewController:addBookViewer animated:YES]
the contact view pops up, but is cut off by the top most bar of the display (the bar showing the AT&T signal strength, clock and battery charge). Is there a way to programatically change the addBookViewer so that it is not cut off by the static status bar?
Thanks,
Dave
It looks like ABPeoplePickerNavigationController inherits from UIViewController. Perhaps you could try editing the view's frame before presenting it:
addBookViewer.view.frame = CGRectMake(0, 20, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 20);
I suspect it is because the view controller that you are calling presentModalViewController:animated: on has a frame that is also being covered, but you don't notice, probably have plenty of head room, make sure you have the status bar in your nib file so that it is laid out correctly.