Home > DeveloperSection > Forums > How to fit datepicker into UIActionSheet in iOS
Tarun Kumar

Total Post:213

Points:1635
Posted on    December-14-2015 7:41 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 432  View(s)
Rate this:

I have created an applicaiton on iPhone, now I want to fit UIDatePicker into the UIActionSheet. I have created a button to show actionSheet. Unfortunately it gets cropped off and the entire Date Picker is not visible. I have not even attempted to add the UIButton yet. Can anyone suggest on getting the entire view to fit properly? I'm not sure how to add the proper dimensions as UIActionSheet seems to lack an -initWithFrame: type constructor.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:

@"Date Picker"
                               delegate:self
                      cancelButtonTitle:@"Cancel"
                 destructiveButtonTitle:nil
                       otherButtonTitles:nil];

// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];

[pickerView release];
[actionSheet release];

I also try with some other code like this:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithFrame:CGRectMake(150.0, 150.0, 70.0f, 70.0f)];

The coords are ofcourse not realistic, but they don't seem to affect the position/size of the UIActionSheet.



Tarun Kumar

Total Post:213

Points:1635
Posted on    December-15-2015 12:07 AM

I get a solution of my code that I asked we can solve this like that:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                  delegate:self
                         cancelButtonTitle:@"Cancel"
                    destructiveButtonTitle:nil
                         otherButtonTitles:nil];
    // Add the picker
    UIDatePicker *pickerView = [[UIDatePicker alloc] init];
    pickerView.datePickerMode = UIDatePickerModeDate;
    [actionSheet addSubview:pickerView];
    [actionSheet showInView:self.view]; 
    [actionSheet setBounds:CGRectMake(0,0,220, 400)];

    CGRect pickerRect = pickerView.bounds;
    pickerRect.origin.y = -100;
    pickerView.bounds = pickerRect;

    [pickerView release];
    [actionSheet release];

But it will better to create a fullscreen view with a UIDatePicker and a navigation bar. For an example see UICatalog -> Pickers sample from the iPhone DevCenter.


Modified On Dec-15-2015 12:17:11 AM

Don't want to miss updates? Please click the below button!

Follow MindStick