Adding a cancel button to the UITextField keyboard

Is there a way to add a cancel button to the keyboard displayed for a UITextField ? Looking back at the UITextInputTraits Protocol Reference, I couldn't find anything, including testing various types of keyboards.

+8
ios uitextfield ios5 uitextinput
source share
3 answers

You can create the appearance of an input that can display a UIToolBar above the keyboard, and then add a cancel button to it. See the documentation link below for the inputAccessoryView property.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html

This is an example of what I did for TextView. The Create Input Accessory View method is called from "textViewDidBeginEditing". Then it creates the appearance of the input and in my case adds three buttons and a space.

I hope this helps.

 -(void)textViewDidBeginEditing:(UITextView *)textView { [self createInputAccessoryView]; [textView setInputAccessoryView:_inputAccessoryView]; self.myTextView = textView; } -(void)createInputAccessoryView { _inputAccessoryView = [[UIToolbar alloc] init]; _inputAccessoryView.barStyle = UIBarStyleBlackOpaque; [_inputAccessoryView sizeToFit]; _inputAccessoryView.frame = CGRectMake(0,_collageView.frame.size.height - 44, _collageView.frame.size.width, 44); UIBarButtonItem *fontItem = [[UIBarButtonItem alloc] initWithTitle:@"Font" style:UIBarButtonItemStyleBordered target:self action:@selector(changeFont:)]; UIBarButtonItem *removeItem = [[UIBarButtonItem alloc] initWithTitle:@"Remove" style:UIBarButtonItemStyleBordered target:self action:@selector(removeTextView:)]; //Use this to put space in between your toolbox buttons UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard:)]; NSArray *items = [NSArray arrayWithObjects:fontItem,removeItem,flexItem,doneItem, nil]; [_inputAccessoryView setItems:items animated:YES]; [_myTextView addSubview:_inputAccessoryView]; } 
+14
source share

I just dropped the UIToolbar into my view controller in Interface Builder, and then:

 @property IBOutlet UIToolbar *keyboardAccessory; -(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // this is so I can edit it in Interface Builder, but it doesn't show in the view [keyboardAccessory removeFromSuperview]; } -(BOOL) textFieldShouldBeginEditing:(UITextField*)textField { textField.inputAccessoryView = keyboardAccessory; return YES; } -(IBAction) pressedCancelButton { [self.view endEditing:YES]; } 

Voila!

+1
source share

Andrew, how is this going? this is Dima P!

As far as I know, there is no great way to do what you are trying to do. You can easily select one of the built-in types listed here.

 typedef enum { UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall, } UIReturnKeyType; 

What you can do is either add an inscription on the keyboard containing the cancel button, or try to go through the hierarchy of views and overlay what you want on the button this way (although this method is not reliable). Unfortunately, this is one that Apple has not yet provided a lot of settings.

edit: Actually, I think from the very beginning I misunderstood your question, since you don’t want the cancel button as the main return button anyway. In this case, the appearance of the input accessory is definitely suitable for the exit.

0
source share

All Articles