Converting ipad app to iphone5 - iphone

How can i modify my code so same app can run on iphone with same UI elements.
I made changes in info Plist and made it universal but Problem is that i have not used stroryboard or nib , xib files to display UI elements. also i hardcoded the height and width for particular button or label ,frame etc.
So is there any way to auto resize screen to ipad view to iphone Or I need to write every code with new height and width for iPhone?
like in android i detect the height and width and according to that i divide it by 2 or 3 to place particular UI item on different screen size.
Also how can i detect that app is running on iphone 4 or iphone 5 or iPad so i can change my images and value or height n width?

You can get the screen bounds using:
[[UIScreen mainScreen] bounds];
The quantity of changes that you would have to make depends on how you designed your code before, if you were using absolute values all the times you'll have much more problems than if you were using relative values...

you can take a look at the UIViewAutoresizingMask property of your views, for example:
view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
as to how to check if you are on iPhone 5 you can use this line of code :
BOOL isIPhone5 = [[UIScreen mainScreen ] bounds ].size.height == 568 ? YES : NO ;

Create seperate nibs for iphone and ipad
well doing it all codewise then you have to set the frame for all the components code wise checking the device is ipad or iphone.
Checking iPad or iPhone look here

Related

iPhone App Size Compatibility

I'm making an iPhone app, and I'm using storyboard for the majority of the UI. I'm using xCode 4.6 for iOS 6.
Is there a way to make the app work for both iphone4 size and iphone5 sizes?
When I run the app on the iPhone 4 simulator it doesn't look like it's supposed to - the UI elements don't look like they do on the storyboard (which I'm assuming uses the iPhone5 size).
Let me know if you need more info.
You can dynamically access the height of the view and the device, and then make adjustments based on those values. Alternatively, you could use iOS 6's constraints to set a margin between the bottom of the device or between other elements.
To achieve the former, just access the height property of the view:
CGFloat height = [[self view] frame].size.height;
You can also get the height of the device's screen like so:
CGFloat deviceHeight = [[UIScreen mainScreen] bounds].size.height;
deviceHeight -= 20; // remove the tab bar
deviceHeight -= 44; // remove height for a navigation bar?
Now imagine adjust your view's origin based on this value. You can make it hug the bottom of the device, no matter which one you're on.
[aView setFrame:CGRectMake(10, deviceHeight - 10 - 100, 300, 100)];
If you're unable to adjust the layout of the elements, consider using a scroll view as well. Just set the frame using the techniques above, and then set the content size. On smaller devices, you'll be able to scroll to see more content whereas on larger devices, it will all be right there.

Application background for iOS application on both iPhone4(s) and 5

I am trying to make a GUI for my first Objective-C application. However, I am kind of stuck at the background.
I need to apply a background which'll work on both iPhone4(s) and 5. How do you do that? I guess resolutions aren't the same on the devices.
There a couple different ways of doing this. One is to create separate storyboards/xibs for each screen size. Another is to use the condition:
if([[UIScreen mainScreen] bounds].height == 568) {
//
}
Or, if your background is a pattern image you only need to apply it once and it will work on both screen sizes, it's as easy as:
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage"]]];
Just create the background image for the size of the iPhone 5 and set the frame for the size of the iPhone 5 as well.
When the app is opened on the iPhone 4 it will truncate the bottom piece of the background-- it won't skew it.
If you are using Storyboards, there is a very useful button in the bottom right corner when the storyboard is open (next to the zoom buttons):
Toggling this will expand/contract your ViewControllers so you can see how it will look for both 3.5 inch (iPhone 4-4s) and 4 inch (iPhone 5) displays.

view size on iphone 5 screen incorrect

I am using in my app the view size to position a subview in code.
for example like this:
self.view.bounds.size.height
This subview has to be animated in and out of the view.
This has worked perfect on older devices, now I am trying to support iphone 5 and found out that I still get the height of the old devices.
Everything except this animated view adapts perfect for iphone 5.
The only way to get the right size is if I change the size of the view in the xib, downside is
that if I now run my app on iPhone 4 the view size is the 4inch view size.
What is the problem here?
Or is this the way it is supposed to be and I have to create an extra xib file for iPhone 5?
Alright so the answer is the following:
Like #mrwalker said make sure that the view automatically or programmatically resizes.
And be aware of the fact that the view is not yet resized in the viedDidLoad method.
(This was my mistake)
If you need the resized views size do your stuff in viewWillAppear there the view has already the right size.
thanks to #mrwalker and #AndyDev
You need to make sure your view is resized for the device it's running on. You could either:
Create a new Xib for the iPhone 5, as you might for an iPad
Have existing Xib automatically or programatically resize
I would only opt for (1) if you were intending on having a different layout (more / fewer buttons and such).
How you achieve (2) depends on whether you're using iOS 6's Auto Layout or the old autoresize model. Both methods can be controlled in the Utilities > Size Inspector in Xcode, or programatically.
If you have a single view & view controller, allowing the view to automatically resize to the parent window should be enough.
I had a similar issue where I wasn't getting the correct height and using the Autolayout / autoresize didn't achieve the desired effect. I used the following code to determine the screen size and made the changes based on this.
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)]) {
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 1136){
// iPhone 5 (1136px height)
} else {
// Not iPhone 5
}
}

Extend app for iPhone 5 - best practice

Now that Apple is about to start shipping iPhone 5's, I'm looking into extending my applications so they appear full screen on the iPhone 5. I ran my apps on the simulator, and even the ones with a UITableView extending to the bottom of the screen, the black bars appear at the top and bottom of the screen.
Here's my question: What is the best practice for extending apps for the iPhone 5?
Should I make a separate nib now for the 3.5 and 4 inch screen, or extend everything in code?
I'm trying to make this as smooth a transition as possible, so I appeal to knowledge of SO to clue me in onto what the best way is to build for both screens.
Taking advantage of the full 4" screen in your apps seems to be as simple as adding a new Default image named Default-568h#2x.png with size 640 x 1136. Xcode 4.5 will actually offer to do this for you automatically by adding a black image of the proper size and name to your project.
For apps that use standard UI components, such as table views, the tables are magically extended to fill the screen. I updated an app for work and it was literally this simple, with only a couple minor issues related to my own code and UI setup. But all in all, very easy.
Contrast this with another app I work on (for myself), which uses none of the standard UI components except for UIViews. To deal with both 3.5" and 4" screens, I have had to spend quite a bit of time refactoring code that needs to know screen size for various operations of the app. (The app in this case is more of a game/simulator than say, a productivity app.)
So, your mileage may vary with regard to the level of effort really required to support the 4" screen. It will depend on how complicated and "non-standard" your app is, as I have discovered.
When updating an existing app for the larger-screen iphone 5, you may also find that the bottom of the screen is not "clickable". For example, if you have a toolbar along the bottom of the screen, that toolbar would be displayed correctly, but buttons would not react to the touch. If this is the case, you need to tell the app that the window is using full screen.
If you have MainWindow.xib, open it, select the window and in the attributes inspector make sure that "Full Screen at Launch" is checked.
If you don't have MainWindow.xib in your project (and most newer projects don't), then you need to add one extra line in the applicationDidFinishLaunching of your app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window setFrame:[[UIScreen mainScreen] bounds]];
...
}
I put it in the beginning of the method. Worked like a charm.
You will find this path with simulator XCode->Targets->Summary->iPhone/iPod Deployment Info( the Retina 4-inch )
You need to add a Default image with size 640 x 1136.
Apply this code in your AppDelegate file
mainWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
mainWindow.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
You will get the main screen size of your window. one more thing whenever you want to check in your class file just add this code
if ([UIScreen mainScreen].bounds.size.height > 500.0f)
For your view controller just add one property autoresizingMask
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
This works fine for me.
I was looking for the same issue, trying to get iPhone 5 (4") to render my xib files to full screen, and I found this answer, which says you only need to add the Default-568h#2x.png image to tell the iOS that it needs to render your app in 4" fullscreen. just adding this fixed the problem for me.
Why [UIScreen mainScreen].bounds] is not returning full screen size?

Struggling with making my app displaying properly on iPad

I have a little trouble grasping the concept of making views that will display proper on iPhone, iPhone 3G(s), iPhone 4 and iPad.
Lets say I have regular UIImageButton at a position of (200, 300), and size is (100, 60).
If I define those values in CGRect structure the button will scale properly on iPhone depending on whether or not it is with iPhone (320px, 480px) or iPhone 4 (640px, 960px). I only need to provide a extra set of images with the doubled size since I am not specifying real pixels but points.
However on iPad this does not work, or else I am doing something wrong because it does not display properly.
I want to know if there is possible to specify that the imagebutton in this example would stay at the same location as on iPhone. I also need to know how I should "properly" initialize a e.g. UIImageButton. Is it correct to say
[[UIImageButton alloc]
initWithFrame:CGRectMake(200.0f,
300.0f, 100.0f, 60.0f)];
if I want to have it located on the same location on the iPad version. Could anyone explain? Thanks in advance.
It's easier to do this in Interface Builder. Select the view, and in the Inspector click on the tab with the ruler (third tab in). The "Autosizing" section lets you control how the location and size of the view changes depending on the device and orientation.
To do this in code, you change the autoresizingMask property of the UIView, e.g.
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight