views:

395

answers:

4

Is the UIPicker that pops up from the bottom of the screen when an action is called just a basic UIPickerView that is coordinated in a certain way? (Like a UIActionSheet?) How would I implement that?

A: 

Put a UIPickerView in a new UIViewController class (let's call it PopupUIPicker) and then display that View Controller via:

PopupUIPicker *pickerController = [[[PopupUIPicker alloc] init] autorelease];
[currentViewController presentModalViewController:pickerController animated:YES];

Where currentViewController is the View Controller you want to display it on. To dismiss it:

[currentViewController dismissModalViewControllerAnimated:YES];

The default animation is to slide up from the bottom. You can change it like:

pickerController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
Matt Williamson
This adds an entire new view. What I am trying to do is just pop up the uiPicker on the bottom half of the screen. (The same thing that happens when somebody clicks the "live feed" button in the upper right hand corner of the facebook app.) A little UIPicker pops up but doesn't cover the entire page.
Rob
You don't have to make the view take up the entire frame.
Matt Williamson
A: 

What do you mean by "the UIPicker that pops up from the bottom of the screen when an action is called"?

If you want to do roughly what the Facebook app does, look up Three20 which is their open-source UI library (no guarantee that the component they use is there; I haven't checked).

tc.
Normally a UIPickerView takes up about half of the screen - depending on where the developer puts it. The facebook app has the UIPickerView come up from the bottom (Just like a UIActionSheet) but it doesn't cover the entire screen.
Rob
A: 

Here's animation code, that I use:

- (void)animateDatePicker:(BOOL)show {
    CGRect screenRect = self.frame;
    CGSize pickerSize = [self.datePickerView sizeThatFits:CGSizeZero];
    CGRect startRect = CGRectMake(0.0,
                                  screenRect.origin.y + screenRect.size.height,
                                  pickerSize.width, pickerSize.height);

    CGRect pickerRect = CGRectMake(0.0,
                                   screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                   pickerSize.width,
                                   pickerSize.height);

    self.datePickerView.frame = pickerRect;
    self.backgroundColor = UIColorMakeRGBA( 64, 64, 64, 0.7f - (int)show * 0.7f );

    if ( show ) {
        self.datePickerView.frame = startRect;
        [self.parentViewController addSubviewToWindow:self];
    }

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationDelegate:self];

    self.backgroundColor = UIColorMakeRGBA( 64, 64, 64, 0.0f + (int)show * 0.7f );

    if ( show ) {
        self.datePickerView.frame = pickerRect;
    } else {
        [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];
        self.datePickerView.frame = startRect;
    }

    [UIView commitAnimations];  
}

datePickerView is a view, that contains DatePicker:

self.datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 174.0, 320.0, 286.0)];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 70.0, 320.0, 216.0)];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker addTarget:self action:@selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.datePickerView addSubview:self.datePicker];

parentViewController should implement addSubviewToWindow method:

- (void)addSubviewToWindow:(UIView *)addView {
    [self.view addSubview:addView];
}

UIColorMakeRGBA:

#define UIColorMakeRGBA(nRed, nGreen, nBlue, nAlpha) [UIColor colorWithRed:(nRed)/255.0f green:(nGreen)/255.0f blue:(nBlue)/255.0f alpha:nAlpha]

slideDownDidStop is a method, that will be called after datePicker slide down successfully.

So, just to summarize - you have MyViewController, that have

MyDatePickerView *myDatePicker;

field.

MyDatePickerView is a custom class, that have UIDatePicker *datePicker field, MyViewController *parentViewController and animateDatePicker method.

When you perform some action on MyViewController (for example, UIControlEventTouchUpInside for some button), you should invoke

[myDatePicker animateDatePicker:YES];

Please let me know if you have questions.

UPDATE: Here's a small example.

kovpas
Wow, thanks for the code - that must have been a lot of work. Unfortunately I don't quite understand how to implement it. I created a basic view application and added another UIViewController called MyDatePickerView. I assume that is supposed to be called from the main view. I copied all of the code from above and declared the method in that view but am not sure how to implement the rest.
Rob
A: 

I have the same issue as as Rob... I can make the UIPicker popup... but it fills the whole screen... i want just the picker to popup... Matt says you don't have to make the view take up the entire frame ... resizing the view along does not work... How do you make just the picker pop up? i was to still see the part of the parent view... much like safari on iphone handles dropdowns.

Ben Call