iOS軟鍵盤遮擋UITableView內文字框問題
1、註冊
UIKeyboardDidShowNotification/UIKeyboardDidHideNotification通知。
-(id) initWithNibName:(NSString*)nibNameOrNil bundle:nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// 寫在這裡,或者viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];
}
return self;
}
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
2、當通知到來,調整frame。
-(void) keyboardShown:(NSNotification*) notification {
_initialTVHeight = _tableView.frame.size.height;
CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil];
CGRect tvFrame = _tableView.frame;
tvFrame.size.height = convertedFrame.origin.y;
_tableView.frame = tvFrame;
}
-(void) keyboardHidden:(NSNotification*) notification {
CGRect tvFrame = _tableView.frame;
tvFrame.size.height = _initialTVHeight;
[UIView beginAnimations:@"TableViewDown" context:NULL];
[UIView setAnimationDuration:0.3f];
_tableView.frame = tvFrame;
[UIView commitAnimations];
}
3、觸發文字框,滾動tableView
-(void) textFieldDidBeginEditing:(UITextField *)textField {
NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:section];
[self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0.5f];
}
-(void) scrollToCell:(NSIndexPath*) path {
[_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
}
demo 下載連結:
http://git.oschina.net/changyou/MyScrollViewKeyBoardDemo/repository/archive/master
5、或許這個更好
- (void)viewDidLoad {
// 監聽鍵盤的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark - 鍵盤通知回撥方法
/*
UIKeyboardAnimationCurveUserInfoKey = 7; 鍵盤執行動畫的節奏
UIKeyboardAnimationDurationUserInfoKey = "0.25"; 鍵盤彈出動畫執行時間
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; 鍵盤尺寸
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 鍵盤當前的位置
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}"; 鍵盤改變後中心的位置
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 鍵盤當前的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; 鍵盤改變後的frame
*/
- (void)keyboardWillChangeFrameNotification:(NSNotification *) notification
{
// NSLog(@"%@",notification.userInfo);
// 1.取出鍵盤當前的y座標
CGRect beginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 2.中間的差值
CGFloat minus = endFrame.origin.y - beginFrame.origin.y ;
// 3.根據差值改變控制器view的frame
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += minus;
self.view.frame = viewFrame;
}
/**
* 拖拽tableView方法
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 退出鍵盤
[self.view endEditing:YES];
}
#pragma mark -dealloc方法寫到最後
- (void)dealloc
{
// 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
參考:
相關文章
- UITableView 鍵盤遮擋UIView
- 輸入框軟鍵盤遮擋問題解決方案
- iOS解決鍵盤彈出遮擋輸入框問題iOS
- 修復蘋果iOS 原生鍵盤遮擋input框蘋果iOS
- 利用flex佈局解決ios輸入框被鍵盤遮擋問題FlexiOS
- 『零行程式碼』解決鍵盤遮擋問題(iOS)行程iOS
- React Native踩坑指南:ios鍵盤遮擋輸入框React NativeiOS
- iOS -UITextField/UITextView之鍵盤遮擋輸入框解決方法iOSUITextView
- react在安卓下輸入框被手機鍵盤遮擋問題React安卓
- flutter - 使用 SingleChildScrollView() 解決鍵盤遮擋輸入框的問題FlutterView
- scrollIntoView與鍵盤遮擋View
- android 記一次解決鍵盤遮擋問題Android
- android記一次解決鍵盤遮擋問題Android
- flutter dialog中軟鍵盤遮擋解決衝突Flutter
- powerpoint: 遮擋文字
- 移動端鍵盤遮擋頁面原理分析
- 安卓和ios鍵盤擋住輸入內容安卓iOS
- 隱藏的輸入框調起軟鍵盤問題--ios/安卓iOS安卓
- ZYKeyboardUtil 全自動處理鍵盤遮擋事件事件
- iOS 文字輸入框隨鍵盤上移iOS
- 直播平臺原始碼,關於彈出框中輸入框被遮擋問題解決原始碼
- Android 軟鍵盤蓋住輸入框的問題Android
- 解決虛擬按鍵遮擋popupWindow
- Android手機軟鍵盤遮蓋輸入框之重新佈局詳解Android
- env(safe-area-inset-bottom)解決ios底部小黑線遮擋問題iOS
- 修復安卓鍵盤彈起擋住輸入框bug安卓
- Win10系統開啟觸控鍵盤會遮擋工作列如何解決Win10
- (資料科學學習手札87)利用adjustText解決matplotlib文字標籤遮擋問題資料科學
- Android 軟鍵盤相關問題Android
- android鍵盤擋住介面Android
- 點選底部input輸入框,彈出的軟鍵盤擋住input(蘋果手機使用第三蘋果
- 點選EditText的時候出現被鍵盤遮擋 在不使用scrollView的情況View
- Android軟鍵盤彈出,覆蓋h5頁面輸入框問題AndroidH5
- Cypress 踩坑記 - DOM 遮擋
- iOS鍵盤彈出時動畫時長失效問題iOS動畫
- 微信小程式使用echarts/資料重新整理重新渲染/圖層遮擋問題微信小程式Echarts
- 【快應用】選單遮擋內容?教你一招快速搞定!
- 谷歌 Chrome 開發者“揭祕”瀏覽器視窗遮擋問題:啟動速度提升25.8%!谷歌Chrome瀏覽器