iOS 解決表單被鍵盤遮住的問題,ios表單鍵盤遮住
問題
處理表單的時候,一定會碰到的就是輸入控制項被鍵盤遮住的問題,:
執行個體
左邊是普通表單,中間是2B表單,右邊是文藝表單.
分析
處理這種問題無非就是2個步驟:
代碼寫出來就是這幾步
那麼如何一步一步的來實現這些步驟呢?
捕獲鍵盤事件
捕獲鍵盤事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
- (void)actionKeyboardShow:(NSNotification *)notification { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; }
- (void)actionKeyboardHide:(NSNotification *)notification { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidShowNotification object:nil]; }
|
計算鍵盤高度並調整
UITableView
的
frame
計算鍵盤高度並調整UITableView的frame
1 2 3 4 5 6 7 8 9 10 11 12 13
|
- (void)actionKeyboardShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height); [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; }
|
擷取當前正在輸入的控制項
這裡得說一句,普通程式員一般是這樣來擷取的
UIView的Category
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
- (UIView *) getFirstResponder { if (self.isFirstResponder) { return self; } for (UIView *subView in self.subviews) { UIView *firstResponder = [subView getFirstResponder]; if (firstResponder != nil) { return firstResponder; } } return nil; }
|
雖然沒錯,但是文藝程式員應該這樣來擷取
UIResponder的Category
1 2 3 4 5 6 7 8 9 10 11
|
static __weak id currentFirstResponder;
+(id)currentFirstResponder { currentFirstResponder = nil; [[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil]; return currentFirstResponder; }
-(void)findFirstResponder:(id)sender { currentFirstResponder = self; }
|
同理,有時候我們需要讓鍵盤消失,那麼也有三種做法可以選擇
1 2 3 4 5
|
[someView resignFirstResponder];
[self.view endEditing:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
|
如何選擇呢? It’s up to U.
計算其在
UITableView
中的位置,並滾動到其位置讓其可見
計算其在UITableView中的位置,並滾動到其位置讓其可見
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
- (void)actionKeyboardShow:(NSNotification *)notification { CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height); UIView *v = [UIResponder currentFirstResponder]; if ( v ) { while ( ![v isKindOfClass:[UITableViewCell class]]) { v = v.superview; } UITableViewCell *cell = (UITableViewCell*)v; [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForRowAtPoint:cell.center] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionKeyboardShow:) name:UIKeyboardDidChangeFrameNotification object:nil]; } |