How to create an iOS Time Picker programmatically?

12k Views Asked by At

I have a tiny little problem.

I'd like to implement the time picker like:

https://i.stack.imgur.com/G7ja3.png

This screen grab is from iOS 6's day notifications of DO NOT DISTURB

My question is how to do it ?

Any ideas ????

Are there any tutorials/resources online that you folks could suggest as how to do this ?

[edited] I should have mentioned that this for an iPhone not an iPad....

Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

This is what I ended up doing:

I found this popover solution -> https://github.com/werner77/WEPopover

That created the popover I wanted, then added this bit of code to the

- (void)viewDidAppear:(BOOL)animated method


    //  place inside a temporary view controller and add to popover

        UIViewController *viewCon = [[UIViewController alloc] init];

    //    PopOverViewController *viewCon = [[PopOverViewController alloc] init];

        UIDatePicker *timePick;
        timePick = [[UIDatePicker alloc]initWithFrame:CGRectMake(5, 0, 0, 0)];
        timePick.datePickerMode =UIDatePickerModeTime;

        [viewCon.view addSubview:timePick];

    //    viewCon.view = label;

        viewCon.contentSizeForViewInPopover = timePick.bounds.size ; // Set the content size

        popover = [[WEPopoverController alloc] initWithContentViewController:viewCon];


        [popover presentPopoverFromRect:CGRectMake(0, 10, 0, 120)
                                inView:self.view
             permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
                              animated:YES];

The only problem now is resizing the picker to the width that I want....other than that, it works!

The solutions that were presented will only work for an iPad not an iPhone....

Thanks everyone.

11
On

Yes.

Just create another view controller with the Time/DatePicker and whatever else you want in it. Use a modal Popover segue connected to your button and use the fixed size option to set the correct size that you want.

Good example here.

Let me know if you need more data.