Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting a UIDatePicker modally

Hey I have a view that has a button that when pressed should modally present a UIDatePicker. I have the picker showing up properly, but because it is on its own UIView, it is full height, and looks funny. All I want is to get a really quick date input from the user, more like a UIActionSheet than a UIView.

How can I make the UIPicker slide up modally only halfway and have some actions on a toolbar, like done etc?

Thanks

like image 243
Jerry Avatar asked Oct 15 '22 00:10

Jerry


1 Answers

You could put it on another UIView which has a transparent background, or more simply, don't use presentModalViewController, but write your own routine to show it in the current view.

 // Untested code:
 // put this in your current UIViewController (the one where you were going to call presentModalViewController:)

 - (void)showPicker:(UIPickerView *) picker{
      CGRect startFrame = picker.frame;
      CGRect endFrame = picker.frame;
      // Set the start position to below the bottom of the visible frame:
      startFrame.origin.y = self.view.frame.size.height;
      // Set the end position to slid up by the height of the view, so it will just fit:
      endFrame.origin.y = startFrame.origin.y - endFrame.size.height;    
      picker.frame = startFrame;

      [self.view addSubView:picker];
      [UIView beginAnimations]
      picker.frame = endFrame;
      [UIView commitAnimations];
 }

You would of course need to add all the necessary code to keep a pointer to the picker and keep track of when to show and get rid of it.

like image 53
Alex Gosselin Avatar answered Nov 03 '22 03:11

Alex Gosselin