I have a keyboard with input accessory view attached, and I used keyboard-Will-Show notification to get height of keyboard.
The problems is at the first time textfield become first responder, the keyboard return 216 for the height (without accessory view's height). But the second time focus on the textfield, value return is 216 + accessory view's height.
How to get the height of keyboard only 216 or 216 + accessory view's height to setup UI frame base on it ?
The top of the keyboard needs to be about 2 inches from the bottom of the *device* as it is held. Prior to the iPhone X, this is easy because all devices used the exact same bezel insets, so it's 216 pts from the bottom of the screen.
I am not sure how you do code, but here is my working code for this :
#pragma mark - Keyboard Notification
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSValue *keyBoardEndFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [keyBoardEndFrame CGRectValue].size;
self.keyboardSize = keyboardSize;
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.keyboardSize = CGSizeZero;
}
- (void) addToolBarToTextView {
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneBtn setFrame:CGRectMake(0, 7, 65, 30)];
doneBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[doneBtn setTitle:@"Next" forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(keyBoardDoneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * barItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = [NSArray arrayWithObjects: flexibleSpace, barItem, nil];
mobileTxtField.inputAccessoryView = toolBar;
}
-(void)viewDidLoad {
[super viewDidLoad];
[self addToolBarToTextView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With