Fix Keyboard Related issue in iOS 11
In iOS 11 there is some predefined function is not working like get the Keyboard Height “UIKeyboardFrameBeginUserInfoKey” sometimes it returns 0 or sometimes works so it is not compatible with iOS 11 . so for solving this, we have used the compatible function which will work in iOS 11 as well as less iOS version devices.
for this follow this steps:
1: In viewdidload
write this method:
1 |
[self registerForKeyboardNotifications]; |
2: Now define this function:
1 2 3 4 |
- (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } |
3: Now write the two methods when the keyboard will show or hide.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
- (void)keyboardWasShown:(NSNotification *)notification { if([addToCartMoved isEqualToString:@"0"]){ addToCartMoved = @"1"; CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; CGRect newFrame = addToCartBlock.frame; _mainViewHeightConstraint.constant += keyboardSize.height + 50; } } - (void)keyboardWillBeHidden:(NSNotification *)notification { if([addToCartMoved isEqualToString:@"1"] ){ addToCartMoved = @"0"; CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; CGRect newFrame = addToCartBlock.frame; _mainViewHeightConstraint.constant -= keyboardSize.height - 50; } } |
4: Now keyboard Height will always return the correct value and now you can use according to their need.