Mapping a UIDatePicker Inside a UIPopover

I use the following code to display a UIDatePicker in a UIPopover , which is displayed when the user clicks the UIButton button.

The problem is that it is displayed in an uncomfortable position, and I want to add a UIToolBar above the datePicker , where there is extra space. This will be the cancel and done button. How can i do this? The button with which it is displayed is Date of Birth .

 - (IBAction)dateOfBirthButtonPressed:(id)sender{ UIViewController* popoverContent = [[UIViewController alloc] init]; UIView *popoverView = [[UIView alloc] init]; popoverView.backgroundColor = [UIColor blackColor]; UIDatePicker *datePicker=[[UIDatePicker alloc]init]; datePicker.frame=CGRectMake(0,44,320, 216); datePicker.datePickerMode = UIDatePickerModeDateAndTime; [datePicker setMinuteInterval:5]; [datePicker setTag:10]; // [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; [popoverView addSubview:datePicker]; popoverContent.view = popoverView; UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; popoverController.delegate=self; [popoverContent release]; [popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO]; [popoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } 

enter image description here

+4
source share
2 answers

Is there a reason you could not just use the UIToolbar ?

 UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 44.0)]; UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @selector(cancel)]; UIBarButtonItem* space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil]; UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action: @selector(done)]; NSMutableArray* toolbarItems = [NSMutableArray array]; [toolbarItems addObject:cancelButton]; [toolbarItems addObject:space]; [toolbarItems addObject:doneButton]; [cancelButton release]; [doneButton release]; [space release]; toolbar.items = toolbarItems; 

Then just add the toolbar to your view. Make sure its size is correct and use the done and cancel selectors.

+5
source
 [popoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

Use sender instead of self.view

 [popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
0
source

Source: https://habr.com/ru/post/1385457/


All Articles