簡體   English   中英

在textField成為第一響應者的同時獲取外部/虛擬鍵盤的事件

[英]Get event for external/virtual keyboard while textField become first responder

在我的iPad應用程序中,我正在展示一個使用表單樣式的控制器

controller.modalPresentationStyle=UIModalPresentationFormSheet;

在橫向模式下,設備的鍵盤打開時會設置tableView的大小,以便用戶可以查看表的所有記錄。

獲取顯示/隱藏鍵盤事件。 我已經設置了NSNotification

問題

但是,當用戶使用外部/虛擬鍵盤點擊表格單元格的textField時,我沒有收到鍵盤顯示/隱藏事件。 因此,當文本字段成為第一響應者時,Tableview的大小會減小,但是在用戶使用外部鍵盤連接時並不需要。

任何人都可以在這里指導/幫助我該怎么辦? 這樣我就可以在使用外接鍵盤時停止設置大小。

注冊鍵盤事件

- (void)registerForKeyboardNotifications{

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasHidden:)
                                             name:UIKeyboardDidHideNotification object:nil];

}

在自動旋轉和文本字段成為第一響應者的同時設置框架

-(void)setFramesOfTable
{

CGRect rct=tableView.frame;
if(appDel.isThisIPad && ([[UIApplication sharedApplication] statusBarOrientation]==UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation]==UIInterfaceOrientationLandscapeRight) && [selectedField isFirstResponder])
{
    rct.size.height=400.0;
}
else
{
    rct.size.height=576.0;
}
tableView.frame=rct;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

selectedField = textField;
[self setFramesOfTable];
}

-(NSUInteger)supportedInterfaceOrientations
{
[self setFramesOfTable];
return UIInterfaceOrientationMaskAll;
}

謝謝。

當文本字段開始編輯時,更改表格的框架不是一個好主意。 在iPad上,用戶可以具有外部鍵盤,擴展塢鍵盤或拆分鍵盤。

如果用戶使用外接鍵盤,則無需調整窗口大小。 使用外接鍵盤時,屏幕鍵盤不會出現,因此沒有理由調整窗口大小。

如果用戶使用拆分鍵盤,則您實際上不必擔心調整窗口大小。 如果他們拆分了鍵盤,則可以將鍵盤放在UI的中間,從而無法(或至少不切實際)重新排列UI,以使其至少不被拆分鍵盤的一小部分所覆蓋。 如果用戶拆分鍵盤並掩蓋了重要的UI組件,則需要將鍵盤移開。

調整UI大小的最佳方法是在鍵盤上使用ChangeFrame / Hide方法

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

在這些事件的處理程序中,您可以獲取鍵盤高度,並相應地調整UI

-(void)keyboardWillChangeFrame:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* kbFrame = info[UIKeyboardFrameEndUserInfoKey];
    NSTimeInterval animationDuration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardFrame = [kbFrame CGRectValue];
    BOOL isPortrait = UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
    CGFloat height = isPortrait ? keyboardFrame.size.height : keyboardFrame.size.width;
}

這將為您提供animationDuration和鍵盤的高度,以便您可以使用UIView animateWithDuration塊為表格更改創建動畫,以便不會被鍵盤遮擋。

在keyboardWillHide中:您只需要從NSNotification(高度顯然為0)中獲取animationDuration(與上述方法相同)。 然后使用另一個UIView animateWithDuration塊為您的tableview設置動畫,使其大小恢復為原始大小

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM