I need to replace an already opened keyboard with datepicker by clicking a button! Searching on internet I'm only finding how to use datepicker as input for a textfield. Is there another way? Thank you!
This should replace the keyboard with UIDatePicker:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[self.someTextField setInputView:datePicker];
Make sure you declare the UIDatePicker:
class UIDatePicker : UIControl
The documentation on more info about the UIDatePicker can be viewed here.
I've created a custom subclass of UITextField which manages a custom datePicker.
I use this code :
self.flatDatePicker = [[FlatDatePicker alloc] initWithParentView:pickerContainer];
_flatDatePicker.datePickerMode = FlatDatePickerModeMonthAndYear;
[self.flatDatePicker show];
self.inputView = _flatDatePicker;
My problem
When I touch my custom text field, the picker pops but my ViewController is moved up. My question is how to remove this movement ? I don't know if it is the normal behaviour of this, but I would like a solution for that.
I launch a keyboard-like interface for UIDatePicker using this code:
_meetingtimeTF.delegate = self;
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[datePicker addTarget:self action:#selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
_meetingtimeTF.inputView = datePicker;
Is there a way to customize the size? Currently the white panel take up the whole horizontal screen even though the real interface is 1/3 of the screen.
How to dismiss the interface after I am done with date picking and click a button? I tried [datepicker setHidden:YES] but but sometime it just hide the datePicker and the white panel remain there.
I have an UITextView and one of my methods requires to change the UITextView inputView to an UIDatePicker, so the user can select a date for a reminder.
Here is the code I used
self.datePicker = [[UIDatePicker alloc] init];
self.noteTextView.inputView = self.datePicker;
[self.noteTextView becomeFirstResponder];
everything works perfectly, but I can't manage to switch back from datePicker to the default keyboard. What I have tried is the following code:
self.noteTextView.inputView = UIKeyboardTypeDefault;
Set the input view to nil
self.noteVite.inputView = nil;
I'm very new to iOS so obviously I'm missing a few concepts when trying to follow this solution Display datepicker on tapping on textfield to my problem.
I have the interface and implementation in a class called DatepickerAppDelegate, but I'm lost here:
"In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view. Connect the outlets appropriately. dateChanged: responds to changes in the date picker and doneEditing: is called when the Done button in the tool bar is clicked. Connect them too. The methods are implemented as listed below."
I read something about XIB's but I'm using a storyboard, so I think I don't have then. Also, I'm not sure how I'm supposed to pull out elements without attaching it to my view controllers (I'm trying, but when I release the mouse button, the tool flies back to the toolbar), and I don't even understand why I need a UIToolbar.
And finally, how do I connect my textfield to the datepicker delegate?
Can someone help me?
UPDATE:
In fact it is very simple, I just need to:
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.birthday.inputView = datePicker;
UIResponder, from which UITextField inherits, defines a property called inputView. This property (a UIView *) can define a view that will be displayed instead of the keyboard when that UIResponder becomes the first responder.
Thus, if you want a UIDatePicker to appear when you tap in a textField (ie, you make that textField the first responder), then you can say, naïvely:
UIDatePicker *datePicker = [[UIDatePicker alloc] init...];
... configure datePicker ...
[myTextField setInputView:datePicker];
Now, when your UITextField is tapped, a UIDatePicker will be brought up instead.
HOWEVER
You'll want to do more than this, because you'll need to handle different orientations and different idioms.
But that's the basic idea.
(And if you want a toolbar above the UIDatePicker, that's what the inputAccessoryView property is for...)
In viewDidLoad of ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[txtFieldBranchYear setInputView:datePicker];
}
Add one method in your ViewController
-(void) dateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
txtFieldBranchYear.text = [NSString stringWithFormat:#"%#",dateString];
}
txtFieldBranchYear is an outlet of UITextField
Add a datepicker view and uitoolbarview to your view.
For example
CGFloat toolbarHeight = 44.f;
CGFloat datePickerHeight = 140.f;
CGFloat containerViewHeight = toolbarHeight+datePickerHeight;
//create container view for datepicker and toolbar
UIView *containerView = [[UIVIew alloc] initWithFrame:CGRectMake(0.f, [self.view bounds].size.height+containerViewHeight, [self.view bounds].size.width, containerViewHeight)];
//after init toolbar set the frame
toolBar.frame = CGRectMake(0.f,0.f,containerView.frame.size.width, toolbarHeight];
//then add on toolbar button save and release it after add
//after init datePicker set the frame
datePicker.frame = CGRectMake(0.f, toolbarHeight, containerView.frame.size.width, datePickerHeight);
//add datePicker and toolBar to containerView
[containerView addSubview: toolBar];
[containerView addSubview: datePicker];
[toolBar release];
[datePicker release];
//add containerView to main view
[self.view addSubview: containerView];
[containerView release];
So when view did load they are will be hidden Y of contener view is outside of the your view frame.
Now you should assign property delegate of UITextFieldDelegate
textfield.delegate = self;
In the method of delegate textFieldDidBeginEditing see UITextFieldDelegate you should fire method that will show your datePicker and toolbar.
If you have many textFields to define which is was selected use tag property, which is integer and you can use switch instead of many if conditions.
To show your datePicker and toolbar you should change the Y of containerView frame.
Here is how to do that
CGRect containerViewFrame = containerView.frame;
containerViewFrame.y -=containerViewHeight;
containerView.frame = containerViewFrame;
To hide just plus the containerViewHeight variable.
You can use UIViewAnimations to show it with some animation. For example UIViewAnimationCurveEaseInOut