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
Related
I have one storyboard file for iPhone devices, and in one of the views there is a subview that contains a UIPickerView, and, when it runs on the iPhone 4 the UIPickerView is stuck at the bottom of the view as it should be, but when it comes to the iPhone 5, the UIPickerView appears a little above of the bottom of the screen.
If I fix the problem for the iPhone 5, the UIVPickerView won't appear completely when I run it on the iPhone 4. (half of it appears below the screen)
So is there a way to have the UIViewPicker well laid for both iphone 4 and 5 resolutions?
NOTE: I solved the issue by creating a completely new subview with the UIPickerView. Now it is well laid whatever the device is.
When creating the picker, you need to use the size of the screen to determine the location. It seems like you are just putting in the coordinates for the y manually, which only works when using one screen size.
If your view that you are in is the same size as the screen, as it most likely is but may not be, you can do:
int y = self.view.frame.size.height;
and use that as the y-coordinate of your pickerView.
Otherwise, you can find the size of the screen by using:
[[UIScreen mainScreen] applicationFrame].size.width //if in portrait
//or
[[UIScreen mainScreen] applicationFrame].size.height //if in landscape
Then you have to subtract the height of the navigationController from that if you have one.
If you're using autolayout, you should be able to change the constraints the picker uses to get it to stick to the bottom. Select the picker, click the "H"-shaped autolayout menu icon in the bottom right corner of the storyboard, and choose "Bottom Space to Superview". Then delete any constraints attaching the picker to the top of the screen. If that doesn't work, make sure that all of the picker's superviews have constraints to attach them to the bottom, too; you'll have to decide whether you want them to resize or slide down on an iPhone 5.
I fixed the issue. Not sure what was going wrong but I deleted the subview, then created a new one and added the UIPickerView. Surprisingly, now it is well laid whatever the device is.
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
Let me start off with saying that I am no expert in iOS Development or Objective-C.
I am making a app for a local charity(its a church) and they want it to support rotation.
The app work as expected if started in portrait mode, I can choose my new subview and the rotate it works perfectly.
However I need it to work so they can rotate at any time. The Main view(the one loaded on startup) already does this out of the box; but my subviews do not.
I have attached 3 screenshots below to try and explain what I mean.
This is how it looks in portrait mode:
In landscape:
and back to portrait it I started the app rotated:
If it helps I add the subview like this:
dailyPrayerView = [[DailyPrayerView alloc] initWithNibName:#"DailyPrayerView" bundle:nil];
[self.view addSubview:dailyPrayerView.view];
Does anybody have any ideas why this is?
Check the autoresizing mask in Interface Builder. It's on the sizes pane of the Utilities pane. You should be able to make it stretch to fit the screen, rather than staying at a constant size.
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?
I'm using the standard UIImagePickerController and using a camera overlay view with it, default controls. I've got an external nib file which I load the overlay view from. The problem is that the view seems to be at 460, so doesn't fully encompass the camera view on iphone 5. I'd like to approach this using auto layout, but I'm not sure how to tell the nib to adjust it's size to be either a height for iphone 4 or iphone 5.
I thought about using setFrame, but that's very un-auto layout. I've also thought about having 2 different nib files, one for iphone 4 and one for iphone 5, but that seems to be the wrong approach too. I'm guessing there's some way to tell the nib to fill the current camera view, but I'm not sure what it is. Can someone recommend the "correct" way to handle this?
You can programmatically load a different cameraOverlayView XIB based on the phone dimensions. The camera controls strip is about 54px tall on 480h screens, and about 96px tall on 568h screens.
Alternatively, you can certainly design your XIB with auto resizing masks set appropriately, but will need to then do a setFrame in code to get the right dimensions.
So either way, you're writing some code to detect the screen bounds and either loading a different XIB or doing a setFrame.
I haven't used nibs for a while now (preferred storyboard) but I believe what you have so far is a UIImageView in the nib, right? Make that an outlet. And then in your m file, check for dimension of the device (many people have asked this recently how to check screen dimension), then you set the uiimageview frame based on the found dimensions. It should work ;)