UIDatePicker keyboard-like interface - ios

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.

Related

Replacing IOS keyboard with Date Picker by button

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.

UITextView with DatePicker

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;

UITextfield UIDatePicker inputView very dark

I have a UITextField that needs to show a UIDatePicker with a UIToolbar in above to close it.
I set the UIDatePicker to the textfield's inputView, and the UIToolbar to the inputAccessoryView.
This works fine, but by default i get weird color effects. The inputAccessoryView is translucent as one would expect from iOS7, however the inputView appears to have a black background behind it that is then made translucent. This leaves it with the inputView much grayer when the background color is set to the same as it is in the inputAccessoryView.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.backgroundColor = [UIColor clearColor];
[self.someTextField setInputView:datePicker];
UIToolbar *pickerToolbar = [[UIToolbar alloc] init];
pickerToolbar.backgroundColor = [UIColor clearColor];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:nil];
pickerToolbar.items = #[doneButton];
[pickerToolbar sizeToFit];
self.someTextField.inputAccessoryView = pickerToolbar;
}
This results in an image like this (with the main view's background colors set in InterfaceBuilder):
You can see the red and green showing through both, but the UIDatePicker is much darker.
If I change both from clearColor to whiteColor, I get this:
Here you can see the inputAccessoryView is still translucent (though less so), but the inputView is pure white. If it is translucent it is a LOT less, and I can't see it anymore.
How can i make them both transparent? As is, difficult without just making both entirely opaque. I want to keep them translucent to follow iOS7 guidelines, but not if it will give me that muddy dark version like in the first screenshot.

Show datepicker on textfield tap

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

blank UIDatePicker on iPad in mode UIDatePickerModeDateAndTime

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.

Resources