如何保持iOS上鍵盤出現時輸入框不被覆蓋,ios輸入框
在 iOS5 上請求顯示鍵盤時,系統從螢幕底部將鍵盤滑入上來,位於應用的內容之上。
(牆內:http://mikixiyou.iteye.com/blog/1488302)
如果螢幕中的內容項目比較多,它就可能覆蓋住文本輸入框之類的對象。你必須調整你的內容,使得輸入框保持可見。
你會想到哪些處理方法呢?
第一種,
臨時調整視窗中各個視圖的大小,使得鍵盤從下向上佔領的地區空白。鍵盤的高度( keyboard.size.height )是一定的,將視圖中所有內容所在地區的 y 值減小到 y-keyboard.size.height 。
該方法有個局限,如果所有內容之和大於視窗減去鍵盤高度的話,該方法將不能用。
第二種,
將視窗中所有視圖嵌入進一個滾動視圖對象( UIScrollView )中。在鍵盤出現時,你將輸入框滾動到合適的位置,調整一下滾動視圖的內容地區。
這些操作通過一個通知 UIKeyboardDidShowNotification 去實現的,邏輯過程如下:
1 、根據通知的字典資訊 userInfo 得到鍵盤的 size 。
2 、根據鍵盤的 size 中的 height 值,調整滾動視圖內容底部的 inset 。
3 、滾動目標視圖即檔案輸入框進入視圖中。
簡要的代碼如下:
1 、實現兩個委託方法,用於指定輸入框對象。
- (void)textFieldDidBeginEditing:(UITextField *)textField |
{ |
activeField = textField; |
} |
| |
- (void)textFieldDidEndEditing:(UITextField *)textField |
{ |
activeField = nil; |
} |
2 、註冊通知的觀察者
- (void)registerForKeyboardNotifications |
{ |
[[NSNotificationCenter defaultCenter] addObserver:self |
selector:@selector(keyboardWasShown:) |
name:UIKeyboardDidShowNotification object:nil]; |
| |
[[NSNotificationCenter defaultCenter] addObserver:self |
selector:@selector(keyboardWillBeHidden:) |
name:UIKeyboardWillHideNotification object:nil]; |
| |
} |
將這個方法放在 viewDidAppear 中調用。
同時也要寫一個 removeObserver 放在 viewWillDisappear 中調用。
3 、實現鍵盤顯示通知的 selector 中的方法
// Called when the UIKeyboardDidShowNotification is sent. |
- (void)keyboardWasShown:(NSNotification*)aNotification |
{ |
NSDictionary* info = [aNotification userInfo]; |
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; |
| |
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); |
scrollView.contentInset = contentInsets; |
scrollView.scrollIndicatorInsets = contentInsets; |
| |
// If active text field is hidden by keyboard, scroll it so it's visible |
// Your application might not need or want this behavior. |
CGRect aRect = self.view.frame; |
aRect.size.height -= kbSize.height; |
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { |
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); |
[scrollView setContentOffset:scrollPoint animated:YES]; |
} |
} |
4 、實現鍵盤消失通知的方法
// Called when the UIKeyboardWillHideNotification is sent |
- (void)keyboardWillBeHidden:(NSNotification*)aNotification |
{ |
UIEdgeInsets contentInsets = UIEdgeInsetsZero; |
scrollView.contentInset = contentInsets; |
scrollView.scrollIndicatorInsets = contentInsets; |
} |
這個方法調整內容底部的 inset 的值使得輸入框不被鍵盤地區屏蔽的。還可以換種方法實現。
第三種,
擴充內容視圖的高度,滾動文本輸入框對象進內容視圖。
將 keyboardWasShown: 重寫。
- (void)keyboardWasShown:(NSNotification*)aNotification { |
NSDictionary* info = [aNotification userInfo]; |
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; |
CGRect bkgndRect = activeField.superview.frame; |
bkgndRect.size.height += kbSize.height; |
[activeField.superview setFrame:bkgndRect]; |
[scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES]; |
} |
IOS開發時,彈出鍵盤擋住輸入框問題
首先需要在代理方法
- (void)textViewDidBeginEditing:(UITextView *)textView
中來處理。
這時需要你移動整個view的frame上移,而不是移動當前的文字框的位置。
為了移動效果的流暢,最好把修改frame的動作放到動畫裡來做
CGRect curFrame=self.view.frame;
[UIView animateWithDuration:0.3f animations:^{
curFrame.origin.y-=需要向上移動的高度;
self.view.frame=curFrame;
}];
鍵盤按鍵不對 是主機電腦 輸入框亂出字元
是你鍵盤下面有灰塵水跡等造成這樣的現象,,也就是連電了,,把你鍵盤清灰吧,,實在不行就換個新的