I have a simple app with multiple UIViews. I've recently begun work to create a universal app out of it get the auto-sizing and rotation of views working with the iPhone 5 and iPad.
On my very first view, the view comes up as the standard 3.5" display with a white bar beneath it on the iPhone 5 (and beside it on the iPad). There is also a toolbar attached to the bottom of the view and it appears at the place where it would be on a 3.5" display. The strange thing is, when I go to another view and then return to the root view, the view is sized correctly.
I've checked my autosizing for the view - all outside and inside anchors are activated. My view mode is set to "scale to fit," and in comparison to other views, settings are the same. And the strange thing is it comes up right the second time the view is displayed.
Any thoughts?
I found my problem. It was in the didFinishLaunchingWithOptions method of the application.
I changed:
UIView *rootView = self.rootViewController.view;
CGRect rootViewFrame = rootView.frame;
rootViewFrame.origin.y +=[UIApplication sharedApplication].statusBarFrame.size.height;
to
UIView *rootView = self.rootViewController.view;
CGRect rootViewFrame = [[UIScreen mainScreen] applicationFrame];
The app was picking up the hard coded size of the view from the XIB when the view first loaded; now I just fit it within the application frame.
Related
I have a picker that I have created in my iPhone app, and it runs fine - but the picker is part of an overlay, so some of the values are hard-coded as to how far down and to the left the overlay appears.
So an obvious error occurs when I try and make the comparable iPad version. I opened the iPad storyboard and set everything up, but naturally when I run it, I have the uipickerview appearing in the middle and to the left of the huge iPad screen, wayyy off where it should be.
Is there any way to make an overlay of a picker automatically center and hug the bottom of the screen, no matter what size the screen is, instead of having the location of it hard-coded?
It looks like you'd like the picker to rest at the bottom of the view controller's main view (it's parent, I assume) and be as wide as the view. Try this...
CGRect viewFrame = self.view.frame;
CGRect pickerHeight = self.picker.frame.size.height; // assume you have an outlet called picker
CGRect pickerFrame = CGRectMake(0.0, viewFrame.size.height-pickerHeight,
viewFrame.size.width, pickerHeight);
self.picker.frame = pickerFrame;
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.
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
}
}
In my iPhone app, I have a UIViewController with two subviews: a main UIView (let's call it mainView), and a secondary UIView (let's call it secondView), which is usually hidden but is sometimes shown (e.g. user settings). In my UIViewController, self.view is set to mainView. When the user switches to the subview, I call [self.view addSubview:secondView] and when the user switches back to the main UIView, I call [secondView removeFromSuperview].
There are two issues which I am dealing with at present:
When the user rotates the iPhone or iPad, the main UIView (mainView) autorotates properly and all of its UI items follow its autoresize rules which I have set up in Interface Builder. However, when the secondary UIView (secondView) is displayed, and the iPhone or iPad is rotated, the secondView & all of its UI items do not follow their autoresize rules. I have found that when I set self.view = secondView, and then rotate the device, the view rotates properly.
In the iPad specifically, when I pull up the secondView using [self.view addSubview:secondView], it is not resized to fill the full screen just as the main UIView has been resized for the iPad version. So I end up with a screen that displays the mainView UIView in the background, and the secondView UIView in the top-left corner without taking up the whole screen.
My instinct tells me that these two issues are related to one another. Has anyone else experienced these kinds of issues with secondary UIViews which they add to their UIViewControllers using addSubview:?
When using addSubview,
[secondView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
Not very elegant. But it worked for me:
I had a similar problem. I solved the size issue by setting the content mode of the view.
Exp:[_yourView setContentMode:UIViewContentModeScaleAspectFill];
I hope this helps...
I am trying to create a mid-size screen for my app on iPad.
In didFinishLaunchingWithOptions(), I do this:
CGRect winRect = CGRectMake(100,100,500,500);
navController.view.frame = winRect;
the screen comes up fine and I can click around and do stuff until the orientation changes. It takes the screen to original full size - how can I make it stick to my winRect frame? I tried setting the parentViewController.view.frame/view.frame to winRect in willRotateToInterfaceOrientation() but no good.
Any ideas?
Thanks.
Generally, you shouldn't mess with the frame of a view controller's view. View controllers tend to resize their views on many occasions, for example when toolbars and navigation bars are hidden or made visible or when the interface orientation changes.
For custom-sized views, create a separate view, set its frame to your custom size and add it as a subview to your view controller's view.