Loading a UIDatePicker in Landscape on a UINavigationController

前端 未结 6 659
慢半拍i
慢半拍i 2021-02-14 20:44

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 a

相关标签:
6条回答
  • 2021-02-14 21:00

    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 ;)

    0 讨论(0)
  • 2021-02-14 21:01

    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.

    0 讨论(0)
  • 2021-02-14 21:02

    The below code will work for both portrait and Landscape mode. For this you need to set your tableview frame origin like that below:-

    _dateTableViewCell=[self.tableView dequeueReusableCellWithIdentifier:@"DatePickerCell"];
    if (self.dateTableViewCell==nil)
    {
        CGRect rect;
        self.dateTableViewCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"DatePickerCell"];
        UIDatePicker *datePicker=[[UIDatePicker alloc]initWithFrame:frame];
        datePicker.datePickerMode=UIDatePickerModeDateAndTime;
        datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        CGRect cellRect = self.dateTableViewCell.frame;
        cellRect.size.height = 216;
        self.dateTableViewCell.frame = cellRect;
        CGRect startRect = self.dateTableViewCell.frame;
        startRect.origin.x = 0.0;
        datePicker.frame = startRect;
        [self.dateTableViewCell.contentView addSubview:datePicker];
    }
    
    0 讨论(0)
  • 2021-02-14 21:03

    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!

    0 讨论(0)
  • 2021-02-14 21:12

    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.

    0 讨论(0)
  • 2021-02-14 21:17

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

    Set self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin

    0 讨论(0)
提交回复
热议问题