大家可能都知道鍵盤上的新Quick Type欄。鍵盤上的QuickType欄
在我的應用程序中,我在鍵盤上放置了一個自定義TextView欄。但由於QuickType欄,我的textview被隱藏。
我想知道,是否有任何屬性或方法知道QuickType欄是否打開?
大家可能都知道鍵盤上的新Quick Type欄。鍵盤上的QuickType欄
在我的應用程序中,我在鍵盤上放置了一個自定義TextView欄。但由於QuickType欄,我的textview被隱藏。
我想知道,是否有任何屬性或方法知道QuickType欄是否打開?
- (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)];
}
沒有什麼可以告訴你QuickType欄是否處於活動狀態,但你可以用這個代碼UIKeyboardWillChangeFrameNotification
註冊通知,並可以獲取有關鍵盤的高度信息。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
使用在通過userInfo
字典UIKeyboardFrameBeginUserInfoKey
和UIKeyboardFrameEndUserInfoKey
值檢索鍵盤的當前和未來的框架。您可以使用以下代碼作爲參考。
- (void)keyboardWillChangeFrame:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
// Manage your other frame changes
}
希望這會有所幫助。每當鍵盤改變幀時它都會被調用。
正如其他答案所建議的,UIKeyboardWillChangeFrameNotification
將觸發每次鍵盤獲得一個新的框架。這包括鍵盤何時顯示和隱藏,以及何時顯示和隱藏QuickType欄。
問題是,這與UIKeyboardWillShowNotification
的通知完全相同,只是名稱不同而已。 因此,如果您已經實施了UIKeyboardWillShowNotification
的方法,那麼您很好。
但有一個例外。當您在處理UIKeyboardWillShowNotification
的方法中獲得鍵盤框時,您必須確保您通過UIKeyboardFrameEndUserInfoKey
訪問它,而不是UIKeyboardFrameBeginUserInfoKey
。否則,當鍵盤顯示/隱藏時,它會得到正確的框架,但當QuickType欄顯示時不會。
所以,在你的處理方法UIKeyboardWillShowNotification
的代碼應該是這個樣子(斯威夫特):
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
}
感謝@Bhumit。我也嘗試過使用UIKeyboardDidChangeFrameNotification。我在下面張貼我的嘗試。這樣對別人也有幫助。 – 2014-09-25 11:35:31