UIDatePicker in UIPopover
Try with below code. It will work fine:
Objective-C
- (IBAction)showDatePicker:(UIButton *)sender {
UIDatePicker *datePicker = [[UIDatePicker alloc]init];//Date picker
datePicker.frame = CGRectMake(0, 0, 320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];//need to implement this method in same class
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor clearColor];
[popoverView addSubview:datePicker];
// here you can add tool bar with done and cancel buttons if required
UIViewController *popoverViewController = [[UIViewController alloc] init];
popoverViewController.view = datePicker;
popoverViewController.view.frame = CGRectMake(0, 0, 320, 216);
popoverViewController.modalPresentationStyle = UIModalPresentationPopover;
popoverViewController.preferredContentSize = CGSizeMake(320, 216);
popoverViewController.popoverPresentationController.sourceView = sender; // source button
popoverViewController.popoverPresentationController.sourceRect = sender.bounds; // source button bounds
//popoverViewController.popoverPresentationController.delegate = self;
[self presentViewController:popoverViewController animated:YES completion:nil];
}
- (void)dateChanged:(UIDatePicker *)datePicker {
NSLog(@"DATE :: %@", datePicker.date);
}
Swift 4.2
@IBAction func showDatePicker(_ sender: UIButton) {
let datePicker = UIDatePicker()//Date picker
let datePickerSize = CGSize(width: 320, height: 216) //Date picker size
datePicker.frame = CGRect(x: 0, y: 0, width: datePickerSize.width, height: datePickerSize.height)
datePicker.datePickerMode = .dateAndTime
datePicker.minuteInterval = 5
datePicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)
let popoverView = UIView()
popoverView.backgroundColor = UIColor.clear
popoverView.addSubview(datePicker)
// here you can add tool bar with done and cancel buttons if required
let popoverViewController = UIViewController()
popoverViewController.view = popoverView
popoverViewController.view.frame = CGRect(x: 0, y: 0, width: datePickerSize.width, height: datePickerSize.height)
popoverViewController.modalPresentationStyle = .popover
popoverViewController.preferredContentSize = datePickerSize
popoverViewController.popoverPresentationController?.sourceView = sender // source button
popoverViewController.popoverPresentationController?.sourceRect = sender.bounds // source button bounds
popoverViewController.popoverPresentationController?.delegate = self // to handle popover delegate methods
self.present(popoverViewController, animated: true, completion: nil)
}
@objc func dateChanged(_ datePicker: UIDatePicker) {
print("DATE :: \(datePicker.date)")
}
Same code will work iPhone also if you implement below delegate respective in view controller
extension YourViewController : UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.none
}
}
How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?
Solved
All I have to do is instantiate the StoryBoard Date Picker View Controller like :
...
DatePickerViewController* popoverContent = [[DatePickerViewController alloc] init];
popoverContent =[[UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil]
instantiateViewControllerWithIdentifier:@"DatePickerVC"];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);
...
where the identifier is set in StoryBoard Attributes Inspector
and now it is shown
UIDatePicker in PopoverView in iPad
pls check the solution provided in the link .. might help you .
1. Create the UIDatePicker programmatically (don’t use the IB)
2. add it to the view after the popup is shown
http://omegadelta.net/2010/06/04/ipad-simulator-crashes-if-a-uidatepicker-is-in-a-uipopovercontroller/
Displaying a UIDatePicker inside of a UIPopover
Is there a reason you couldn't just use a 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 to size it correctly and implement the done
and cancel
selectors.
uidatepicker in uipopover delegate
You are absolutely correct one way to get the value in view controller is to implement delegate functions. These are very easy if you understand at the technical level. I will try to explain it here.
you have to define the protocol in the datePickerViewcontrollerClass.h like this
@protocol TimePopupViewControllerDelegate <NSObject>
-(void)returnSelectedDate:(NSDate*)date;
@end
and create a instance of 'id' type for passing the reference of mainViewController like this.
@property (nonatomic, assign) id < TimePopupViewControllerDelegate > delegate;
in MainViewController.m where you are creating instance of datePickerViewcontrollerClass, you have to set the delegate like this
datePickerViewcontrollerClass *myViewControllerForPopover =[[datePickerViewcontrollerClass alloc] init];
myViewControllerForPopover.delegate = self;
in the method where you getting the date from picker in datePickerViewcontrollerClass.m class you have to pass it to main class using delegate.
-(void)viewDidDisappear:(BOOL)animated{
[_delegate returnSelectedDate:datepicker.date];
[super viewWillDisappear:animated];
}
you can write this in any method I have written in ViewWillDisappear or any other method.
After this in MainViewController this method get called and you can retrieve the selected date
-(void)returnSelectedDate:(NSDate *)date{
}
Technically you are passing the referece of mainViewController instance to your datePickerViewcontrollerClass and calling the methods on mainViewController from datePickerViewcontrollerClass
I hope I will be able to explain it clearly if you still any doubt you can comment.
UIDatePicker in iPhone
I think I may have found the key to my problem. After reading through this document on the various places to place orientation-related code, I decided on viewWillLayoutSubviews
. I read through the document again and decided to try putting that code in viewWillAppear
instead, and that seems to have resolved the problem.
The change to using viewWillAppear
also required implementation of willAnimateRotationToInterfaceOrientation:duration:
It looks like viewWIllLayoutSubviews
is needed for iPad but not iPhone, so I've also added this:
-(void)viewWillLayoutSubviews
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//iPhone code goes here (empty, since calling showMonthContainingDate: doesn't work from here in iPhone
} else {
//ipad code goes here
NSDate *dayToDisplay = self.displayedDate;
[self showMonthContainingDate:dayToDisplay];
//iPad uses wrong dimensions unless the routine is called from here.
}
}
One final note. There is one scenario where the orientation of the device is not set correctly -- that is landscape on an iPhone at launch. According to this answer to another question the iPhone always launches in portrait unless it is a landscape-only app.
Display UIDatePicker With ViewController (modalPresentationStyle = UIModalPresentationPopover)
UIPopoverController deprecate in iOS 9.x version . Only just show warning you can replace UIPopoverController to UIPopoverPresentController and use that controller properties and delegates method
Read from below
link
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverController_class/index.html
Read from below link
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/
Drag Datepickerviewcontroller in storyboard select Datepickerviewcontroller after that drag uidatepicker, button, create IBoutlate And Action . Datepickerviewcontroller Connect with segue of firstviecontroller button and select segue go to Attribute Inspactor section and select the animation style popover presenting type
Related Topics
How to Create a Simple Checkbox in iOS
iOS Autolayout with Uiscrollview: Why Does Content View of Scroll View Not Fill the Scroll View
Adjust Uibutton Font Size to Width
Objective-C 101 (Retain VS Assign) Nsstring
How to Display a Uipopoverview as a Annotation to the Map View? (iPad)
What Should Image Sizes Be at @1X, @2X and @3X in Xcode
Using Custom Font in a Uiwebview
Difference Between _ and Self. in Objective-C
Custom Uitableviewcell Programmatically Using Swift
Alternative iOS Layouts for Portrait and Landscape Using Just One .Xib File
Showing Pushviewcontroller Animation Look Like Presentmodalviewcontroller
Presenting Camera Permission Dialog in iOS 8
iOS 5.1 with Xcode 4.2 and Retina in iPad 3
iPhone Read Uiimage (Frames) from Video with Avfoundation
How to Disable Rotation in React Native
What Is Prefix.Pch File in Xcode
How to Use Apple's New .P8 Certificate for Apns in Firebase Console