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;
}
Related
UIWebview scalesPageToFit is not working properly when running an iPhone app on iPad with iOS 7.
I set the scalesPageToFit = YES before loading the request to the WebView.
After the page loads, inspecting the HTML document width gives 769px while the UIWebView's scroll view frame width is 320. The scroll view zoomScale is 1 although you would expect it to be 0.41... (320/769). Any idea?
The problem is now fixed in iOS 7.0.3. But, if you can't go there, please read on.
This seems to be a defect in iOS7. To recap, the problem happens when you run a iPhone only app, compiled with iOS7 SDK, in a iOS7 iPad or iPad Mini. A temporary work around is to scale the scroll view of the web view. This makes the text look smaller than you will like, but, so far, this is the best solution I have seen.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize contentSize = webView.scrollView.contentSize;
CGSize viewSize = self.view.bounds.size;
float scale = viewSize.width / contentSize.width;
if (scale < 0.9) {
NSLog(#"Zoom out fix for web view: %f", scale);
webView.scrollView.minimumZoomScale = scale;
webView.scrollView.maximumZoomScale = scale;
webView.scrollView.zoomScale = scale;
}
}
We have an ios-app and a webapp with the same integration problem. We had to solve this in the webapp by enforcing the 320 css. I consider this an ios7 bug and would expect a fix for this.
Same problem: https://twitter.com/lukew/status/380702676121825280
I never used Interface Builder ever in my app, and don't think it is something good to do.
My app was already designed for iPad/iPad2 layout, without ever considering making it work on iPhone4/iPhone5/iPAD-mini. Now I want my app to work on all of them.
I think it would be extremely ugly to have 4+ layout the universal app, like this:
if(ipad or ipad2)
view.frame = CGRectMake(0,0,100,100);
else if(ipad-mini)
view.frame = CGRectMake(0,0,100,100);
else if(iphone4)
view.frame = CGRectMake(0,0,100,100);
else if(iphone5)
view.frame = CGRectMake(0,0,100,100);
else if(iphone6)
view.frame = CGRectMake(0,0,100,100);
else if(iphone7)
view.frame = CGRectMake(0,0,100,100);
There should be only 2 layout ideally, but I don't know how to implement this way. Those fixed coordinates would not stretch on different screen sizes
if(ipad or ipad2 or ipad-mini)
view.frame = CGRectMake(0,0,100,100);
else if(iphone4 or iphone5 or iphone6 or iphone7)
view.frame = CGRectMake(0,0,100,100);
How do I do a universal app in the best way?
when you started building this app you should have done soemthing like this:
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
and you change above options according to your needs.
and yes you would need different for ipad, iphone
First of all, your layout for iPhone will almost certainly be drastically different from iPad. For the iPads, all the different types have the same aspect ratio, so all you need to worry about is having the #2x images for the iPad with retina.
For the iPhone, however, you have the iPhone 4/4s VS the iPhone 5. Also, you have a lot less screen space, so you will need to find ways to present the same information in a smaller space. This can be done using scrollviews, tableViews, and having one part of the information in one viewController that you can switch to from another viewController.
Finally, I would highly recommend interface builder for most cases. It takes care of iPhone vs iPhone 5, and portrait vs landscape for you. It will even automatically animate the transition when rotating the phone. Sometimes, however, when you need to do custom drawing or our layout is really hard to get with interface builder, it is better to do it programmatically. In that case, a good way to check which device would be by checking the size of [[UIScreen mainScreen] applicationFrame]. Check that with the values of 1024x768 for an iPad, 320x480 for iPhone, or 320x568 for iPhone 5.
However, you really should use interface builder (for most cases). Why exactly do you want to avoid it?
I'm super confused about how the iPhone 4 and below apps are optimised for the iPhone 5. I'm a designer. How is optimisation done? Basically I have an app with loads of knobs and buttons and other interactive components. The way I see it, there are 3 options but what is the best way (or least, standard practice)?
1) Keep the layout the same for iPhone 5 but just add extra length on the background.
2) Scale the images/layout of components from iPhone 4 to iPhone 5 so everything is proportional.
3) Have completely separate images and different layouts for both. i.e for iPhone 5, I can move components to utilise the space more. The problem with this (and I'm not a developer) is that the interaction position of the components have moved so in effect, the iPhone 4 and 5 are separate apps?
To identify if it is a iphone 5 iphone, use this code in the file nameYourApp-prefix.pch:
//Macro iPhone5
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
find the nameYourApp-prefix.pch file in the supporting files, the code must be written between:
# ifdef __ OBJC__
//Code Macro iPhone5
# endif
and then just use the "if" to check that device, like this:
if (IS_IPHONE_5) {
//Code for 4 inch screen
}else{
//Code for 3.5 inch screen
}
putting the macro in the nameYourApp-prefix.pch, all classes see it.
With a UIKit application, none of your options are ideal. You simply use Springs and Struts or Auto Layout to have your user interface elements snap to where they should be.
The solution is the same when switching between portrait and landscape orientation. You define which user interface elements can grow and which ones can't, which edges to snap to, etc.
Run it on the simulator to test the layout if you don't have an iPhone 5 device.
For example, for a UITableViewController, the UITableView should grow vertically on an iPhone 5 so that you see more rows.
I almost always have at least one control that would be nice to grow given the room. If you don't, then you'll probably just have more empty space at the bottom of the view.
How iPhone 5 Optimization Works
When you tell iOS that your app is optimized for the iPhone 5 (through use of a cleverly named image file), iOS uses your existing Springs and Struts or Auto Layout (whatever you're using) to rearrange the interface.
The first choice is the easiest and works pretty well. You'd only need to worry about having specific assets for full screen images, and make sure that NavigationBar and TabBar (in case your app have one), remains on top and bottom respectively.
Scaling other images/components such as buttons, you'll change the general aspect ratio (which you probably wouldn't want), since iPhone 4&5 display sizes only differ in height.
For developing app option two is better. Although you dont have to change all the images for iPhone 5 some images will me same like TabBarItem. But you have to create background image different for iPhone4 and 5.
Read out this apple document to create user interface.
And you can differentiate iPhone 4 from iPhone 5 using this code
UIBtton * btn = [[UIButton alloc] init];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
btn.frame = CGRectMake(60,60, 205, 175);
//this is for iPhone5
}
else
{
btn.frame = CGRectMake(60,50, 199, 170);
//this is for iPhone4
}
Here my button place and length are different for iPhone 5 and iPhone 4
And set your control's hight and width according to iPhone Screen Size.
If you didn't, adjust your view layouts with proper auto resizing masks or look into Auto Layout if you only want to support iOS 6 going forward. You have to check height of [[UIScreen mainScreen] bounds].
Example:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen (iPhone 5)
} else {
// code for 3.5-inch screen (< iPhone 5)
}
Note that [UIImage imageNamed:#"yourImage.png"] will only load either "background.png" or "yourImage#2x.png", it will not load "yourImage-568h#2x.png" if it exists.
Check Auto-Rotation API as Well.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do I support the taller iPhone 5 screen size?
I am new to iPhone and I have a small doubt. Now the iphone5 screen height is 568 and previous iphone's screen height was 480. How can we implement apps for iphone5.Should we check views or controllers all the time for iphone5 and below versions? If am wrong Please correct me.
Thanks in advance.
here are the points
*if you want to make use of full height of the iPhone5 create splash screen that's exactly 640x1136 pixels in size and name default-568h#2x.png
*if you have problem with rotation check
a)window.rootViewController = firstViewcontroller (dont add view to your window)
b)implement the new rorate delegate function
*Use viewDidLayoutSubviews rather than viewDidLoad to set up widget sizes relative to the window
*You can manually check the screen size and load the right image like this:
UIImage* myImage;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {
myImage = [UIImage imageNamed:#"myImage-568h#2x.png"];
} else {
}
myImage = [UIImage imageNamed:#"myImage.png"];
or use catogary from the following GIT gist
https://gist.github.com/3719620
Just use
CGRectGetHeight([UIScreen mainScreen].applicationFrame)
CGRectGetWidth([UIScreen mainScreen].applicationFrame)
to get screen's height & width.
in your project (in XCode 4.5) goto "Supporting File" group (which is automatically created).
you got to use those Default#2x.png/Default-568h#2x.png files. !!
That is the key change required for the OS to size the window to fill the iPhone 5 display. Redth# has posted a writeup on this and other size-related tweaks you might need to make.
!!
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.