Dismiss a numeric keypad from a UITextField w/o a UIToolbarButton - iphone

I know there has been some discussion here regarding How to show button ‘Done’ on number pad on iPhone OS 4? to dismiss a number pad. I would like to find a way to dismiss the number keypad w/o having to add a UIToolbarButton. My UITextFields are zipcode and age.The mint.com iPhone app does this; so there has to be a way.
Here is my current code that adds the UIToolbarButton:
textField1.keyboardType = UIKeyboardTypeNumberPad;
textField1.delegate = self;
//Create a toolbar to add to the textpad
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleDefault;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissZipKeyPad)],
nil];
[numberToolbar sizeToFit];
textField1.inputAccessoryView = numberToolbar;

I used this to dismiss the keypad when the user touches anywhere outside the text field:
- (void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]])
[view resignFirstResponder];
}
}
I found it here:
http://remarkablepixels.com/blog/2011/1/23/dismiss-iphone-keyboard.html

Related

Setting animation to always come up

I made a textfield who's first responder is a datePicker. I also attached a toolBar to to a view, so when the textfield is tapped, the toolbar slides up with an animation. Then on the toolBar their is a done button that resigns the first responder. I also want it to remove the toolBar. To do this I added this
[pickerBar removeFromSuperview];
Then to reopen it I did this
[self.view addSubview:pickerBar];
So it is activated when the textview is touched.
The problem is when I tap the textfield again the toolBar loses its navigation.
Heres most of the code
pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle=UIBarStyleBlackOpaque;
[pickerBar sizeToFit];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
CGRect pickerBarRect = CGRectMake(0, 568, 320, 44);
[pickerBar setFrame:pickerBarRect];
} else {
CGRect pickerBarRect = CGRectMake(0, 480, 320, 44);
[pickerBar setFrame:pickerBarRect];
}
//Set up the new position of the frame of the view
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(releasePicker)];
[barItems addObject:doneBtn];
[barItems addObject:doneBtn];
[pickerBar setItems:barItems animated:YES];
}
}
}
- (void)releasePicker {
[textfield resignFirstResponder];
[pickerBar removeFromSuperview];
}
- (void)pickerActivated {
[UIView animateWithDuration:.4 animations:^(void) {
[self.view addSubview:pickerBar];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){
CGRect rect = CGRectMake(0, 266, 320, 44);
[pickerBar setFrame:rect];
pickerBar.hidden = NO;
} else {
CGRect rect = CGRectMake(0, 178, 320, 44);
[pickerBar setFrame:rect];
}
}];
}
There is a difference between inputView and inputAccessoryView. This is what you want:
// setting the inputView
UIPickerView *pickerView = [[UIPickerView alloc] init];
// other configurations //
self.textField.inputView = pickerView;
// setting up the inputAccessoryView
UIToolbar *pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle = UIBarStyleBlackOpaque;
[pickerBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(releasePicker)];
[pickerBar setItems:#[flexSpace, doneBtn] animated:YES];
self.textField.inputAccessoryView = pickerBar;
This way the tool bar will be "attached" to your inputView and will automatically come up when the the text field is activated and disappear when your picker does!
Edit/Update
I did find this link that may help you, if you are interested in using a IB focused approach:
https://stackoverflow.com/a/10705161/1429262
Far simpler is to use the inputAccessoryView property on UITextField. It is irrelevant whether your inputView is a keyboard or a picker. Set your toolbar as the inputAccessoryView and it will animate in on top of your picker with the done button and then animate out on resignFirstResponder.

Keyboard and PickerView both are visible

Im having 3 textfield in one i have implemented a UIPickerView and disable the keyboard using resignFirstResponder , but when i click on the other, both Keyboard and pickerview are visible .how do i sort this ?
create local instance of UIPickerView, and in delegate method of TextFiled textFieldDidBeginEditing check for visibility of UIPickerView, if it is Visible hide it
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(![_pickerView isHidden])
{
[_pickerView setHidden:YES];
}
//...
}
set every UITextField's Delegate to self and then just put this method in your .m class.. In this code i just set frame of yourPickerView with 500 y point instead of that you can Hide the UIPickerView..
This code Hides UIPickerView with Animation like Keyboard..
UPDATE:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == TextField1) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = yourPickerView.frame;
frame.origin.y = 500;
yourPickerView.frame = frame;
[UIView commitAnimations];
return YES;
}
else if (textField == TextField2) {
[TextField1 resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect frame = yourPickerView.frame;
frame.origin.y = 107;
yourPickerView.frame = frame;
[UIView commitAnimations];
return NO;
}
return YES;
}
You need to know what to show exactly. You can use [self.view endEditing:YES]; in order to remove the keyboard when necessary, and implement your own method to hide the picker when needed.
Set the picker as the inputView for the textfield:
myTextField.inputView = self.myPickerView;
This way you don't need to handle the displaying of the keyboard/picker at all. It's all done for you.
Simplest Solution : inputView Property
yourTextField.inputView = self.yourPickerView;
Check the Documentation.
You need to Put this line First when you are going to Display the PickerView.
[self.view endEditing:YES];
Now after that PickerView Will open & focus will not be on the TextField after writing following line so what you need to do is Put Done Button over Picker & pass the Selected value of Picker to that TextField.
Try this Code:
- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
if(txtVisitDate.isEditing == TRUE)
{
if ( [txtPlaceName isFirstResponder] )
{
[txtPlaceName resignFirstResponder];
}
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 475)];
}
}
Hope this will help.

How to add a view on top of keyboard?

I have added a view with buttons on top of keyboard using setInputAccessoryView.
Now i want functionality like when i click button from the view i want to display a pickerView on top of keyboard. Means like adding pickerView as subview of keyboard.
How can i do this?
Make a view you want to at the top of the keyboard. You can do this from xib or manually, but I think xib will be a better option.
Then make the reference to this view by doing this
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.inputAccessoryView = YOURCustomView;
return YES;
}
Whenever you want to hide this or remove this just put this
textField.inputAccessoryView = nil;
self.pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 194, 320, 224)];
self.pickerContainerView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.pickerContainerView];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
self.lblPickerViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 230, 30)];
self.lblPickerViewTitle.backgroundColor = [UIColor clearColor];
[self.lblPickerViewTitle setFont: [UIFont fontWithName:#"Arial-BoldMT" size:17]];
self.lblPickerViewTitle.textAlignment = UITextAlignmentLeft;
self.lblPickerViewTitle.textColor =[UIColor whiteColor];
[pickerToolbar addSubview:self.lblPickerViewTitle];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(closePickerView:)];
[barItems addObject:btnCancel];
[btnCancel release];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:#"ShowPicker" style:UIBarButtonItemStyleBordered target:self action:#selector(donePickerView:)];
[barItems addObject:btnDone];
[btnDone release];
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
[self.pickerContainerView addSubview:pickerToolbar];
And in ShowPicker Method , write code for display UIPicker
You need to create another UIWindow first:
UIWindow *newWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,100,320,20)];
newWindow.windowLevel = UIWindowLevelStatusBar; // this will make to place your WINDOW above the keyboard
[newWindow makeKeyAndVisible]; // show the new window
// [newWindow addSubview:yourView];
like add button on keyboard you can also add view like bellow...
here you find window of keyboard and then set frame of button and also yourView and then add to keyboard
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)note {
UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
returnBtn.frame = CGRectMake(0,-25,320,25);
returnBtn.adjustsImageWhenHighlighted = NO;
returnBtn.backgroundColor=[UIColor darkGrayColor];
returnBtn.titleLabel.textColor=[UIColor whiteColor];
[returnBtn setBackgroundImage:[UIImage imageNamed:#"keyBtn.png"] forState:UIControlStateNormal];
[returnBtn addTarget:self action:#selector(keyboardBtn:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
// if (txtTag==5) {
[keyboard addSubview:returnBtn];
}
}
-(IBAction)keyboardBtn:(id)sender{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES)
// if (txtTag==5) {
[keyboard addSubview:yourView];// here add your view with frame
}
}
i hope this help you...
:)
From Jonas Schnelli's answer
UIWindow *newWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0,100,320,20)];
newWindow.windowLevel = CGFLOAT_MAX; // this will make to place your WINDOW above the keyboard
[newWindow addSubview:yourView];
Just change UIWindowLevelStatusBar to CGFLOAT_MAX will be ok.

Is it possible to add done button inside the keyboard in iphone?

Currently i am working in iPhone application, Using UITextField to enter phone number, user enter the phone number, then how to hide the keyboard? because no return button here, Is it possible to add done button inside the keyboard, please help me
Thanks in Advance
Here is to be a good tutorial about this.
Refer this link. It shows how to add "Done" button in key board.
But I would suggest you to use inputAccessoryView instead. Refer this SO post.
And Must read this SO answer.
You can add inputAccessoryView with 'Apply' and 'Cancel' buttons, and dismiss the number pad with then.
inputAccessoryView
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Apply" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = #"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
Without using done button you can also hide numeric keyboard this way by touching anywhere on the view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[yourtextfieldname resignFirstResponder];
}

number pad keyboard not show return key in iphone

I am new to iPhone technology. I've saved a problem in a iPhone application, but when i use textfield keyboard type number pad the keyboard doesn't show a return/done button.
How can I hide the number pad keyboard after entering the number in the textfield? Do you have any solutions for that. Please provide help.
You can do this in this way:
#property (nonatomic, strong) UITextField *textField;
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonDidPressed:)];
UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])];
[toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]];
self.textField.inputAccessoryView = toolbar;
}
- (void)doneButtonDidPressed:(id)sender {
[self.textField resignFirstResponder];
}
+ (CGFloat)toolbarHeight {
// This method will handle the case that the height of toolbar may change in future iOS.
return 44.f;
}
you must be using UIKeyboardTypeNumberPad, try this instead UIKeyboardTypeNumbersAndPunctuation,
It'll not only show the return key but also provide you with some extra punctuations
There is no return or done key in number pad. You can do one thing when user touch outside of the textfield you can hide the keyboard. You should do something like this -
if (there is a touch outside of your textField)
{
[textField resignFirstResponder];
}
This question has already been answered, I know because thats how i got the solution. You have 2 options, first is to hide the keyboard by implementing a touch on the mainview that will send the "finished editing" message. that Hides the keyboard [self.view endEditing:YES];
If you do add the touch listener to the mainview you have to implement a condition so that any other buttons keep working.
What you do want to do to simulate a return key is to actually add it like this:
Register for a keyboard did show notification and add this to the code:
if ([self.passCodeLabel isFirstResponder])
{
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
//doneButton.frame = CGRectMake(0, 163, 257, 257);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"doneup.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"donedown.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
NSLog(#"%#",[[UIApplication sharedApplication] windows]);
UIView* keyboard;
NSLog(#"Shared applicaiton windows count:%i",tempWindow.subviews.count);
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
NSLog(#"%#",[keyboard description]);
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:#"<UIPeripheralHostView"] == YES)
{
NSLog(#"Adding return button");
[keyboard addSubview:doneButton];
}
}
}
This will add your own "done" button image to the keyboard (which you can just copy by taking a screenshot of the screen of the blank slot and adding the done text).
Btw the code i pasted works on my layout. For yours you might have to modify it a bit, but the principle is the same.
Create the button
Look for the keyboard subview
Add the button to that subview
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
_txt_mobileno.inputAccessoryView = numberToolbar;
}
-(void)doneWithNumberPad
{
[_txt_mobileno resignFirstResponder];
}