views:

853

answers:

5

I have a custom UINavigationController that supports auto-rotate. I also have a UIDatePicker on one of my views that I throw onto the stack of the Navigation controller. The auto-rotate works if I start the date picker view in portrait and then rotate it. If I try load the date picker view in landscape to begin with, the view is all messed up. It looks like it would if it didn't support rotation and the frame only has about half of the picker visible and off center.

I've tried making my own date picker that supports auto-rotate in case that was the problem, I've tried creating two different views and swapping them out, and I've tried changing the view frame size on the ViewWillAppear method. None of them seem to be working for me as of yet.

Anyone have any suggestions on how to get the date picker to show up in landscape correctly on a navigation controller? I probably am overlooking something simple and the answer is right in front of me, but I'm not finding it.

A: 

Well I fixed it. I managed to make a check to see what the orientation was, and then remove the date picker and in code, I recreated a date picker and added it. That worked, it wasn't beautiful, but it worked.

Apparantly it doesn't render properly if the device is already in landscape orientation when the view controller is loaded from the nib. So your fix to recreate the control makes sense, but I found a nicer fix, see my answer.
progrmr
A: 

Add date picker to view that is auto-rotated (controller's view, not directly in the window).

Set self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin

porneL
+1  A: 

I ran into the same problem. When you load a view while already in Landscape orientation the UIPicker is not rendered correctly. I spent several hours trying to find a workaround until I found a fix from this page from Llamagraphics. You add this code in viewDidLoad of the view controller that has the picker:

for (UIView* subview in myPicker.subviews) {
    subview.frame = myPicker.bounds;
}

Visit the webpage for more details. Thank you Stuart!

progrmr
This works great!
morais
A: 

Here's what worked for me. When I create the DatePicker, I make it think it's in portrait mode:

[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait  animated: NO];
datePicker = [[UIDatePicker alloc] initWithFrame: CGRectZero];
[[UIApplication sharedApplication] setStatusBarOrientation: curOrientation animated: NO];

So basically, I change the orientation, create the datePicker, and change the orientation back right away to what it was before. I set the picker width (screen width) and height (216) elsewhere.

I got this clue from another page.

A: 

I had the same problem with displaying the date picker (in timer mode) resized horizontally on the iPad. I couldn't get any of the known solutions to this bug to work, so I fixed this with an ugly hack which involved putting two datepickers in the view - one is resized in the background, and the other is in standard size (320px) in the foreground... I know, sounds sick, but it displays properly and that's what counts. It's Apple's fault anyway ;)

Psionides