What is the height of the Apple System Keyboard in iOS8/iOS8? - ios

What is the height of the Apple System Keyboard in iOS 8 onwards with & without the predictive text bar which lies on top of the keyboard?

Use
NSValue* keyboardFrame = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
for detail value
Add an observer of UIKeyboardWillChangeFrameNotification to the viewDidLoad;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
the delegate method
- (void)keyboardWillChange:(NSNotification *)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrame = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSLog("value ==%#",keyboardFrame);
}

Related

keyboard notification not called in ios 11.3

I have keyboard notification in my App and its working fine in ios 10 but my notification method not called in ios11.3
Below is my code:
- (void)attach {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification {
UIView* responder = [self findFirstResponder];
if (responder) {
_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapAnywhere:)];
[baseView addGestureRecognizer:_tapRecognizer];
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect screenRect = [RGLayout layout].fullScreen;
CGRect frame = responder.frame ;
}
}
I have tried to find the solution and i got that the only changes in iOS11 is UIKeyboardFrameEndUserInfoKey for keyborad height.
But my problem is that my keyboardWasShown not called in ios11.3 same code is working ios 10.2
FYI: when user click on next the next textField will becomeFirstResponder.
Don't know why but the behaviour is strange the notification is calling in one screen and not in another screen.
I have solve the issue by following code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (self.nextField) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.nextField becomeFirstResponder];
[[NSNotificationCenter defaultCenter] postNotificationName: UIKeyboardDidShowNotification object:self];
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
[textField resignFirstResponder] ;
});
}
return NO ;
}

Obj-c - Wrong keyboard height is returned?

I'm trying to get the height of the iOS keyboard so that I can move my view up when the keyboard is animated. However, I keep getting the value 258 returned (which, isn't correct, as it pushes my view up too high?) Why is this happening? Code below:
ViewController.m
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)keyboardWillChange:(NSNotification *)notification {
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
self.keyboardHeight = keyboardFrameBeginRect.size.height;
}
- (void) animateTextView:(BOOL) up
{
const int movementDistance = self.keyboardHeight;
const float movementDuration = 0.3f;
int movement= movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.upView.frame = CGRectOffset(self.upView.frame, 0, movement);
[UIView setAnimationDidStopSelector:#selector(afterAnimationStops)];
[UIView commitAnimations];
}
To get height of keyboard, you should use UIKeyboardWillShowNotification or UIKeyboardDidShowNotification instead of UIKeyboardWillChangeFrameNotification.
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
}
If you use UITabBarController, you have to calculate frame without tab bar height. You can get tab bar height with the code below.
self.tabBarController.tabBar.frame.height
You should register UIKeyboardWillShowNotification for observing the keyboard height.
1) [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
2) you can also try this code :
(void)keyboardWillChange:(NSNotification *)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
}

Get Keyboard size in textFieldShouldBeginEditing delegate

I am having trouble while i am creating a messaging app, When keyboard is opened i am not sure about total size and frame for Keyboard (dictionary area is opened or not).
I want to get total size and frame in
textFieldShouldBeginEditing
delegate.
You should use the UIKeyboardWillChangeFrameNotification . Also make sure to convert the CGRect into the proper view, for landscape use.
Set NSNotificationCenter in your textFieldShouldBeginEditing
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
and write this method.
- (void)keyboardWillChange:(NSNotification *)notification {
CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; //this is it!
}
In Swift 4
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_noti:) ), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
KeyboardWillChange Method
#objc func keyboardWillChange(_noti:NSNotification)
{
let keyBoard = _noti.userInfo
let keyBoardValue = keyBoard![UIKeyboardFrameEndUserInfoKey]
let fram = keyBoardValue as? CGRect // this is frame
}
Register for UIKeyboardWillShowNotification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
and in the selector get the keyboard frame:
- (void)keyboardWillShow:(NSNotification *)iNotification {
NSDictionary *userInfo = [iNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
}
I have faced this problem and solved by using notification observer on keyboard.
//set observer in textFieldShouldBeginEditing like
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myNotificationMethod:) name:UIKeyboardWillChangeFrameNotification object:nil];
return YES;
}
// method implementation
- (void)myNotificationMethod:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
NSLog(#"%#",keyboardFrameBegin);
NSLog(#"%f",keyboardFrameBeginRect.size.height);
}
//remove observer
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[[NSNotificationCenter defaultCenter ]removeObserver:self];
return YES;
}

iOS Keyboard show event handling

I am working on a chat app wherein I have a textview (not textfield) and when I click on it, the keyboard should show and everything should move up.
Till now, I have managed to shift the frame of the table view and textview up and show the keyboard using the below code.
- (void)keyboardWasShown:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGPoint contentViewOrigin = self.contentView.frame.origin;
CGFloat contentViewHeight = self.contentView.frame.size.height;
CGRect visibleRect = self.view.frame;
visibleRect.size.height -= keyboardSize.height;
BOOL up = CGRectContainsPoint(visibleRect, contentViewOrigin);
if (!up){
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,self.tableView.frame.origin.y,self.tableView.frame.size.width,280.0f);
self.contentView.frame = CGRectOffset(self.contentView.frame, 0, 0 - keyboardSize.height);
if([self.tableView numberOfRowsInSection:0]!=0)
{
NSIndexPath* ip = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0]-1 inSection:0];
[self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:UITableViewRowAnimationLeft];
}
}
}
- (void)keyboardWillBeHidden:(NSNotification *)notification {
self.contentView.frame = originalContentView;
self.tableView.frame = originalTable;
}
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)deregisterFromKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
But when I saw how whatsapp does it, mine looked like a hack. Whatsapp's keyboard moves up together with all the elements while mine works like this: First the keyboard is shown, a notification is sent to the app, notification is received, the code calculates the height of keyboard and moves up the elements according to the height.
I have searched and found the solution that I have implemented.
Can someone help??
I use this trick a lot in my apps. You want to listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification.
The best way to handle animation in my opinion is using autolayout. When you call [self.view layoutIfNeeded]; your views will move along with the keyboard animation. No animation block needed.
I've set up a simple project for anyone to try and see how it works!
- (void)addKeyboardNotificationsObserver {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
NSDictionary* info = [paramNotification userInfo];
//when switching languages keyboard might change its height (emoji keyboard is higher than most keyboards).
//You can get both sizes of the previous keyboard and the new one from info dictionary.
// size of the keyb that is about to disappear
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// size of the keyb that is about to appear
CGSize kbSizeNew = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//make adjustments to constraints here...
//and here where's magick happen!
[self.view layoutIfNeeded];
}
- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
//adjust constraints
[self.view layoutIfNeeded];
}
You can use UIKeyboardWillShowNotification and UIKeyboardWillHideNotification
Try TPKeyboardAvoidingScrollView from here: https://github.com/michaeltyson/TPKeyboardAvoiding (my choice)

Whatsapp style - keyboard appear under the toolbar

How do I do it whatsapp style, where the message space, when clicked, would push a keyboard up from the bottom, as well as pushing the toolbar up as well. And then when cancelled (i.e. clicking in the background), it'll push the keyboard back down with the toolbar
This is done with the UIKeyboardWillShowNotification, UIKeyboardDidShowNotification, UIKeyboardWillHideNotification and UIKeyboardDidHideNotification notifications.
Then when you handle the notification you adjust the height of the frame:
For example:
- (void) keyboardWillShow:(NSNotification *)aNotification{
NSDictionary* info = [aNotification userInfo];
NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGFloat keyboardHeight = [aValue CGRectValue].size.height;
self.keyboardVissible = YES;
[UIView animateWithDuration:duration animations:^{
CGRect frame = self.contentView.frame;
frame.size.height -= keyboardHeight;
self.contentView.frame = frame;
}];
}
You will need to register to receive the notification, you should only listen to the keyboard notification when the view is visible, strange thing could happen if you do it in the viewDidLoad:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

Resources