QuickType Bar on the Keyboard

2019-03-05 16:47发布

As you all might be knowing about the new Quick Type bar on the keyboard.

In my application , I have put a custom TextView bar on the keyboard. But because of QuickType Bar , my textview gets hidden.

I want to know , Is there any property or method to know whether the QuickType Bar is open or not ?

3条回答
【Aperson】
2楼-- · 2019-03-05 17:11

There is nothing which can tell you whether the QuickType bar is active or not but you can register for the UIKeyboardWillChangeFrameNotification notification with this code and with that you can get info about the height of the keyboard.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];

Use the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey values in the passed userInfo dictionary to retrieve the current and future frames of the keyboard. You can use the following code as a reference.

- (void)keyboardWillChangeFrame:(NSNotification*)notification
{
     NSDictionary* keyboardInfo = [notification userInfo];
     NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
     CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
     // Manage your other frame changes
}

Hope this helps. It will get called every time the keyboard changes its frame.

查看更多
爷的心禁止访问
3楼-- · 2019-03-05 17:11

As the other answers have suggested, the UIKeyboardWillChangeFrameNotification will fire every time the keyboard gets a new frame. This includes when the keyboard will show and hide, and when the QuickType bar will show and hide.

The thing is, this is exactly the same notification as the UIKeyboardWillShowNotification, just with a different name. Therefore, if you're already implementing a method for the UIKeyboardWillShowNotification, you're good to go.

With one exception, though. When you get the keyboard frame in your method for handling the UIKeyboardWillShowNotification, you must make sure you access it via the UIKeyboardFrameEndUserInfoKey, not the UIKeyboardFrameBeginUserInfoKey. Otherwise, it will get the correct frame when the keyboard is shown/hidden, but not when the QuickType bar is.

So, the code in your method for handling the UIKeyboardWillShowNotification should look something like this (in Swift):

func keyboardWillShow(notification: NSNotification) {
    let info = notification.userInfo!
    keyboardRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    keyboardRect = yourView.convertRect(keyboardRect, fromView: nil)

    // Handling the keyboard rect by changing frames, content offsets, constraints, or whatever
}
查看更多
聊天终结者
4楼-- · 2019-03-05 17:25
- (void)keyboardFrameChanged:(NSNotification*)aNotification
{

    NSDictionary* info = [aNotification userInfo];
    CGPoint from = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].origin;
    CGPoint to = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin;


    float height = 0.0f;
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        height = to.x - from.x;
    } else {
        height = to.y - from.y;
    }

    [self setContentSize:CGSizeMake(self.frame.size.width, self.frame.size.height + height)];
}
查看更多
登录 后发表回答