Reputation: 2357
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 ?
Upvotes: 1
Views: 2879
Reputation: 16318
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.
Upvotes: 4
Reputation: 394
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
}
Upvotes: 3
Reputation: 2357
- (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)];
}
Upvotes: 2