5

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.

B.S.
  • 21,660
  • 14
  • 87
  • 109

4 Answers4

4

You can change Setting autocorrectionType to UITextAutocorrectionTypeNo on the UITextView (or correction to NO in IB) disables autocorrect as well as the predictive text bar in iOS 8. There doesn't appear to be a way to disable just the predictive bar however.

yourTextView.autocorrectionType = UITextAutocorrectionTypeNo;

enter image description here

Edit:

check this I think it is gonna be helpful

Community
  • 1
  • 1
Omarj
  • 1,151
  • 2
  • 16
  • 43
  • It is already mentioned by OP "I do not want to change textView's autocorrectionType. I am fine with that bar, just want to handle it in correct way." then how come this is the solution. – Vizllx May 27 '15 at 09:09
  • Disabling autocorrection will hide this bar. I want to handle this bar in appropriate way, but seems like Apple doesn't provide this way. – B.S. May 27 '15 at 09:19
  • @B.S. what do u mean by "handle it in appropriate way" what do u want to do with it ? – Omarj May 27 '15 at 09:28
  • @B.S. Apple doesn't document everything, we should learn from there Example/Sample Project they provide in there developer site.. all of us can learn many things out of that. Anyways my answer already have the solution how to handle the bar. – Vizllx May 27 '15 at 09:50
3

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.

Vizllx
  • 9,135
  • 1
  • 41
  • 79
1

You can get the keyboard height by adding an observer:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChange:", name: UIKeyboardDidChangeFrameNotification, object: nil)
//Method
func keyboardWillChange(notification: NSNotification){
        println(notification.userInfo?.description)

 }
Ajay
  • 1,622
  • 20
  • 36
0

In my case, the issue was I was not considering about bottom safe margins, I had to subtract bottom safe margins to set content size properly when appearing keyboard. Might help someone struggling to calculate keyboard height without predictions.

let window = UIApplication.shared.keyWindow
bottomInsets = window?.safeAreaInsets.bottom ?? 0
Tharindu Madushanka
  • 3,241
  • 7
  • 31
  • 33