UIDatePicker in UIActionSheet on iPad
Asked Answered
F

1

15

In the iPhone version of my app, I have a UIDatePicker in a UIActionSheet. It appears correctly. I am now setting up the iPad version of the app, and the same UIActionSheet when viewed on the iPad appears as a blank blank box.

Here is the code I am using:

UIDatePicker *datePickerView = [[UIDatePicker alloc] init];
    datePickerView.datePickerMode = UIDatePickerModeDate;

    self.dateActionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose a Follow-up Date"
                                                   delegate:self cancelButtonTitle:nil 
                                     destructiveButtonTitle:nil otherButtonTitles:@"Done", nil];

    [self.dateActionSheet showInView:self.view];
    [self.dateActionSheet addSubview:datePickerView];
    [self.dateActionSheet sendSubviewToBack:datePickerView];
    [self.dateActionSheet setBounds:CGRectMake(0,0,320, 500)];

    CGRect pickerRect = datePickerView.bounds;
    pickerRect.origin.y = -95;
    datePickerView.bounds = pickerRect;
Fibrilla answered 9/5, 2011 at 19:34 Comment(5)
Question helped me understand how Apple expected users to use Popovers and ActionSheets on the iPad much better than their documentation did. Thank you.Deviate
Check my answer it may use full to you. https://mcmap.net/q/712422/-uidatepicker-in-uipopoverPrying
Thanks @Narayana , but this question was from 6 years ago :-)Fibrilla
@Fibrilla yes my question also 6 years ago i have updated my answer with swift so added this comment.Prying
Check this answer https://mcmap.net/q/712422/-uidatepicker-in-uipopover/7343106#7343106Prying
F
21

I ended up creating a separate segment of code for the iPad Popover:

//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];

datePicker.frame = CGRectMake(0, 44, 320, 300);

[popoverView addSubview:toolbar];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;

//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);

//create a popover controller
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

//present the popover view non-modal with a
//refrence to the button pressed within the current view
[popoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem 
                          permittedArrowDirections:UIPopoverArrowDirectionAny 
                                          animated:YES];

//release the popover content
[popoverView release];
[popoverContent release];
Fibrilla answered 10/5, 2011 at 15:4 Comment(2)
This is great, however I get the empty white box above the date picker which is expected. Does anyone know how I can use this real estate somehow? I'd like to set a title here.Course
@Course you might just need to set a negative value on a CGRect so that a Label will appear in the unused space - hacky but it might just work. :-/Lanceolate

© 2022 - 2024 — McMap. All rights reserved.