dismissing keyboard for a specific to textfield
[self.myTextField1 resignFirstResponder];
[self.myTextField2 resignFirstResponder];
[self.myTextField3 resignFirstResponder];
dismissing keyboard for the entire application
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
dismissing keyboard for the entire view
[self.view endEditing:YES];
forcing keyboard for a textfield
[self.textField becomeFirstResponder];
clear button while user is editing, other modes available
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
events when the keyboard appears or hides
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
...
- (void)keyboardWillHide:(NSNotification*)notification
{
NSLog(@"keyboard will hide");
}
-(void)keyboardWillShow:(NSNotification*)notification
{
NSLog(@"keyboard will show");
}
ignore user input
[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; // stop user interaction
[[UIApplication sharedApplication] isIgnoringInteractionEvents]; // check if interaction is being ignored
[[UIApplication sharedApplication] endIgnoringInteractionEvents] // resume user interaction
finding the keyboard view ONLY when its present on screen (null other times)
-(UIView*)findKeyboardView
{
for (UIWindow* window in [UIApplication sharedApplication].windows)
for (UIView *possibleKeyboard in window.subviews)
if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"])
return possibleKeyboard;
return nil;
}
different keyboard types
mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert; // dark background
mytextfield.keyboardAppearance = UIKeyboardAppearanceDefault;
mytextfield.keyboardAppearance = UIKeyboardAppearanceLight;
mytextfield.keyboardAppearance = UIKeyboardAppearanceDark;
For a Number Pad type keyboard you cannot add a done button… thanks apple!!!!
so the only solution is to create a toolbar with a dismiss type button.
if you create the UIToolBar in the storboard view, youll need to hide it before the keyboard takes control
-(void)viewDidLoad
{
[super viewDidLoad];
[self.toolbarDone removeFromSuperview];
...
Adding a toolbar created in code on top of the keyboard
with UIBarButtonItem created from UIButton
// create the toolbar
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,320,50)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
self.textFieldForCodeMadeToolbar.inputAccessoryView = toolbar;
self.textFieldForCodeMadeToolbar.keyboardAppearance = UIKeyboardAppearanceAlert;
// create the buttons for the toolbar
NSMutableArray* toolbarButtons = [[NSMutableArray alloc] init]; // mutable array
for (int i=0; i<10; i++) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 20, 30);
button.layer.backgroundColor = [UIColor grayColor].CGColor;
button.layer.borderColor = [UIColor grayColor].CGColor;
button.layer.borderWidth = 1.0f;
[button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[toolbarButtons addObject:barButton];
}
toolbar.items = toolbarButtons; // set the buttons to the immutable array
...
-(IBAction)barButtonTapped:(UIButton*)btn
{
NSLog(@"button tapped %@", btn.titleLabel.text);
}
You may (and I prefer to) also create a toolbar in storyboard and link to a textfields keyboard action
Make sure your toolbar outlet created is Strong
@property (strong, nonatomic) IBOutlet UIToolbar *keyboardToolbar;
Remove it from the superview (issue with ios8)
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextField.inputAccessoryView = self.keyboardToolbar;
[self.keyboardToolbar removeFromSuperview]; // needed in ios8
...
}