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.
Related
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 am currently developing a prototype that I want to do user testing on desktop first before loading to iPad.
I am looking for solutions to disable the keyboard after clicking a textfield. That means after clicking a textfield, user is able to enter information from the macbook keyboard directly, and the virtual keyboard that automatically shows up in the simulator will not appear. I have been through a lot of tutorials but they are all dismissing the keyboard after user entry; that is not what I am looking for. How should I hide the keyboard?
Thanks so much!
Use this:
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor
It works for UITextField and UITextView and they need to be set to editable.
What you did Here:
You created a dummy view of width=hight=0, & assigned it as the inputView of your textField.
How It works:
Instead of showing default, keyboard, now, the viewController is showing DummyView as inputView for your UITextField. As DummyView has Width=height=0, You will not see anything on the screen :)
Here is another answer which I found the same hack but with little additional supportive code snippet to hide the blinking cursor too.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hides both keyboard and blinking cursor.
}
I needed this to be done for a Quantity text field where I increase/decrease the quantity using a UIStepper view. So I needed the keyboard to be hidden always.
This will set the inputView of your textField to, basically, an empty UIView with no frame.
self.theTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
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
I am struggling with the UIDatePicker on iPad. The setDate or .date assignment makes the DatePicker empty:
UIDatePicker * dtVCpicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
dtVCpicker.datePickerMode = UIDatePickerModeDateAndTime;
dtVCpicker.date = [NSDate date];
dtVCpicker.minimumDate = [NSDate date];
[self.view.window addSubview:dtVCpicker];
if I change the mode to UIDatePickerModeDate or UIDatePickerModeTime then it would look normal.
Thanks a lot for your answers!
I implemented the work-around for this issue: setting the dtVCpicker.date inside the viewDidLoad made the Picker displayable.
In my existing code, I just added the picker inside a Popup, and so viewDidLoad wasn't called when the Popup displays. So I created a view that contains the picker, and put the view inside the Popup (and put the setDate in the viewDidLoad). It works.
The problem is with the line:
dtVCpicker.date = [NSDate date];
This causes the UIDatePicker with UIDatePickerModeDateAndTime to break (probably a bug). However, this line is needless, since the date picker is by default set to current date.
To sum up, in my code I simply solve the issue by removing the above line. Of course, it is still possible to set the date of the picker, but in the other method.