iOS 處理鍵盤遮擋TextField、TextView問題

來源:互聯網
上載者:User

iOS 處理鍵盤遮擋TextField、TextView問題
iOS 處理鍵盤遮擋TextField、TextView問題   之前處理鍵盤遮擋問題都是在每一個控制器進行單獨處理,這樣做真的是非常的費事,今天在做項目的時候就想到自己封裝一個,記錄一下這個“跌宕起伏”的過程。   思路是這樣的:計算文本編輯控制項Frame與鍵盤Frame,如果遮擋則移動控制器View。   建立控制器類:WKAvoidKeyboardViewController    

 1 #import <UIKit/UIKit.h> 2  3 @interface WKAvoidKeyboardViewController : UIViewController 4  5 @property (nonatomic, strong) UITextField *editTextField; 6 @property (nonatomic, strong) UITextView *editTextView; 7  8 - (void)hideKeyboard:(NSNotification *)noti; 9 - (void)showKeyboard:(NSNotification *)noti;10 11 12 @end13 14 15 #import "WKAvoidKeyboardViewController.h"16 17 #define GetOSVersion [[UIDevice currentDevice].systemVersion floatValue]18 19 #define GetTransformDistance(Distance) (GetOSVersion < 7.1 ? Distance / 2 : Distance)20 21 @interface WKAvoidKeyboardViewController ()<UITextFieldDelegate, UITextViewDelegate>22 23 @end24 25 @implementation WKAvoidKeyboardViewController26 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event27 {28     [self.view endEditing:YES];29 }30 @end

 

   步驟1:通過通知擷取當前編輯的文本控制項  
//註冊通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];- (void)textFieldBeginEditing:(NSNotification *)noti{    self.editTextField = noti.object;    self.editTextView = nil; }- (void)textViewBeginEditing:(NSNotification *)noti{    self.editTextView = noti.object;    self.editTextField = nil;}

 

    步驟2:通知擷取鍵盤高度   步驟3:計算是否需要移動    
#pragma mark - 鍵盤躲避- (void)showKeyboard:(NSNotification *)noti{    self.view.transform = CGAffineTransformIdentity;    UIView *editView = _editTextView ? _editTextView : _editTextField;        CGRect tfRect = [editView.superview convertRect:editView.frame toView:self.view];    NSValue *value = noti.userInfo[@"UIKeyboardFrameEndUserInfoKey"];    NSLog(@"%@", value);    CGRect keyBoardF = [value CGRectValue];        CGFloat animationTime = [noti.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];    CGFloat _editMaxY = CGRectGetMaxY(tfRect);    CGFloat _keyBoardMinY = CGRectGetMinY(keyBoardF);    NSLog(@"%f %f", _editMaxY, _keyBoardMinY);    if (_keyBoardMinY < _editMaxY) {        CGFloat moveDistance = _editMaxY - _keyBoardMinY;        [UIView animateWithDuration:animationTime animations:^{            self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, -moveDistance);        }];            }}- (void)hideKeyboard:(NSNotification *)noti{    //    NSLog(@"%@", noti);    self.view.transform = CGAffineTransformIdentity;}

 

  初步實驗:UITextFiled成功,然後到了UITextView,坑爹的問題粗線了=.=, UITextViewTextDidBeginEditingNotification 發送時間是在鍵盤彈出通知之後的,導致第一次點擊TextView沒有用,點擊第二次才能產生效果。於是乎,我又開始嘗試用TextView的Delegate來做,想當然的使用的代理方法 - (void)textViewDidBeginEditing:(UITextView *)textView 1 - (void)textViewDidBeginEditing:(UITextView *)textView2 {3     4 }  令人失望的是textViewDidBeginEditing:方法調用依然是在鍵盤通知彈出後再調用,此時心中想的是:嗶了狗了,讓人怎麼玩!還是看看其他方法吧。於是在代理方法中看到了 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView   嘗試之後,此方法的確在鍵盤彈出前調用,大功告成,接下來就是設定代理的問題了   設定代理方法如下: 
 1 - (void)searchTextViewWithView:(UIView *)view 2 { 3     for (UIView *subview in view.subviews) 4     { 5         if ([subview isKindOfClass:[UITextView class]]) { 6             ((UITextView *)subview).delegate = self; 7         } 8         if ([subview isKindOfClass:[UITextField class]]) { 9             ((UITextField *)subview).delegate = self;10         }11         [self searchTextViewWithView:subview];12     }13 }

 

   至此大功告成,使用方法:繼承WKAvoidKeyboardViewController,如果是用故事版建立的文本控制項,啥都不用做,如果是用代碼建立的,則需要在ViewDidLoad中調用searchTextViewWithView方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.