iOS 解決表單被鍵盤遮住的問題,ios表單鍵盤遮住

來源:互聯網
上載者:User

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];
}
計算鍵盤高度並調整 UITableViewframe

計算鍵盤高度並調整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];

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.