Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a button to hide keyboard

On a UITextView to hide the keyboard, there is the method:

...
    textfield.returnKeyType = UIReturnKeyDone;
    textfield.delegate = self;
....

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;

}

but if I want to leave the button "done" to the "return" and add a button to hide the keyboard, how do I?

like image 614
Vins Avatar asked May 30 '11 18:05

Vins


People also ask

How can I hide my keyboard while typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen.

How do I unhide keyboard on Android?

It's in the "Keyboards & input methods" section of the menu. Tap Null Keyboard. Now, when you tap in a text field, no keyboard will appear. Tap a different keyboard under Current keyboard to re-enable the on-screen keyboard.


3 Answers

You can assign a toolbar with a button that dismisses the keyboard as the text field's inputAccessoryView. A quick example would be,

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
toolbar.items = [NSArray arrayWithObject:barButton];

textField.inputAccessoryView = toolbar;
like image 124
Deepak Danduprolu Avatar answered Oct 06 '22 07:10

Deepak Danduprolu


Swift 2.0 version:

//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()

//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:)))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar

Swift 4:

//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()

//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar

func donePressed() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

UIToolBar Documentation

'inputAccessoryView' documentation

like image 39
Jon Vogel Avatar answered Oct 06 '22 07:10

Jon Vogel


This can be done ways easier!

I made a custom view in IB, in my viewController.h I just made an IBOutlet UIView *accessoryView;, connected them and an - (IBAction)dismissKeyboard;

I put in the view a toolbar with a done button, made a connection to the IBAction an wrote: [textView resignFirstResponder] and

- (void)viewDidLoad
{
    textView.inputAccessoryView = accessoryView;
    [super viewDidLoad];
}

But actually that looks a bit strange and non-apple-style… Got an idea?

like image 37
Julian F. Weinert Avatar answered Oct 06 '22 06:10

Julian F. Weinert