I have some instance of UITextView
and I want this textView
to occupy all empty vertical space when keyboard is shown. The problem is that I do not know what height will keyboard take as it has predictive bar in iOS 8 and its actual height can be changed by user when keyboard was already shown. I do not want to change textView
's autocorrectionType
. I am fine with that bar, just want to handle it in correct way.
So the question: Is there any possibility to know if this bar is visible or not? Is there any was to trigger user swipe to show/hide this bar?
Thanks in advance.
Well you can handle when the user swipe to show/hide the bar(prediction bar) of UIKeyboard,
First step
declare a keyboard notification in viewdidLoad()
and declare a global variable kbSize
float kbSize;
- (void)viewDidLoad
{
kbSize=0.0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
--- rest of your code here---
}
Second step
Now in keyboardWillShowNotification() method do the following
#pragma mark - Notifications
- (void)keyboardWillShowNotification:(NSNotification *)aNotification{
NSDictionary *infos = aNotification.userInfo;
NSValue *value = infos[UIKeyboardFrameEndUserInfoKey];
CGRect rawFrame = [value CGRectValue];
CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
if(kbSize==0.0)
{
kbSize=keyboardFrame.size.height;
NSLog(@"prediction bar is visible");
}
else if(keyboardFrame.size.height<kbSize)
{
NSLog(@"prediction bar is not visible");
--- rest of your code here, how you want to change your view when bar is not visible ---
}
else
{
NSLog(@"prediction bar is visible");
--- rest of your code here, how you want to change your view when bar is visible ---
}
}
Conclusion:- As keyboardWillShowNotification will always fire whenever the user does some editing with your text-fields or whenever user will swipe to hide or show the prediction bar.
So we just have to check the height of keyboard whenever keyboardWillShowNotification
will fire,so if the user swipe the bar to hide then automatically the keyboard height will decrease and we are already storing the height of keyboard in variable kbSize
, we just have to check whether current height of keyboard is less than then the stored kbSize
. So by this way we can check at runtime whether bar is visible or not or user swipe to hide the bar.