iOS -UITextField/UITextView之鍵盤遮擋輸入框解決方法

weixin_33670713發表於2017-12-05

大多的解決辦法如下所述:


法 1. 將整個介面上的控制元件和view檢視都新增到UIScrollView滑動檢視中,然後由UIScrollView控制元件實現可以上下滑動從而輸入框不被軟鍵盤覆蓋遮擋。
法 2. 通過一個通知 UIKeyboardDidShowNotification 去實現的,需要用到事件監聽,而且需要自己定義並實現“將要開始編輯”與“結束編輯”這兩個監聽事件中的方法。
法 3. 當出現鍵盤和關閉鍵盤的時候檢視向上或向下移動一定距離可以使所見檢視不被遮擋。

當然方法很多,上面的具體操作,這裡不貼出可以mark的程式碼。還將進一步mark出下面的解決方法和程式碼。
一、第一種解決方法,程式碼如下:
   //開始編輯輸入框的時候,軟鍵盤出現,執行此事件 
  //這是在使用textField的時候呼叫textField.delegate = self;呼叫代理,自動進入這個方法
 -(void)textFieldDidBeginEditing:(UITextField *)textField 
    { 
        CGRect frame = textField.frame; 
//改進方法:CGRect frame = [textField convertRect:textField.frame toView:self.view];
//否則的話對於textField是巢狀在別的view中的情況,self.view上移幅度不夠
        int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0-35.0); //鍵盤高度216,鍵盤上的toolBar高度為35;textField高度32,如果這個控制元件位於另一個檢視上還需要加上父檢視的Y值,才可以是需要的偏移量。          
        NSTimeInterval animationDuration = 0.30f; 
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
        [UIView setAnimationDuration:animationDuration];          
//將檢視的Y座標向上移動offset個單位,以使下面騰出地方用於軟鍵盤的顯示 
        if(offset > 0) 
          self.view.frame = 
            CGRectMake(0.0f, -offset, self.view.frame.size.width,self.view.frame.size.height);          
        [UIView commitAnimations]; 
    }      

//當在鍵盤上按下return鍵或者按Enter鍵,使得keyboard軟鍵盤消失

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
        [textField resignFirstResponder]; 
        return YES; 
    }      

//輸入框編輯完成以後,將檢視恢復到原始狀態

-(void)textFieldDidEndEditing:(UITextField *)textField 
    { 
        self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
    }  
二、 第二種解決方法,程式碼如下:
 1. 首先在.h中使用<UITextFieldDelegate>代理協議,或者在.m的類擴充套件中使用<UITextFieldDelegate>UITextField控制元件代理協議

 2. 新增文字輸入框控制元件,在@interface 類擴充套件中宣告兩個屬性:
        @property (nonatomic, weak) UITextField * userNameText;
        @property (nonatomic, weak) UITextField * userPwdText;
        
 3. 新增實現佈局UITextField控制元件. 
        UITextField * userNameText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"賬號"];
        userNameText.delegate = self;
        [self.view addSubview:userNameText];
        self.userNameText = userNameText;
        UITextField * userPwdText = [AutolayoutViewautolayoutTextFieldWithPlaceholder:@"密碼"];
        userPwdText.delegate = self;
        [self.view addSubview:userPwdText];
        self.userPwdText = userPwdText;

 4. 實現相應協議代理方法
            此處主要解決:
    // 當前點選textfield的座標的Y值 + 當前點選textFiled的高度 - (螢幕高度- 鍵盤高度 - 鍵盤上tabbar高度)
    // 在這一步就是這個當前textFiled的的最大Y值和鍵盤的最全高度的差值,用來計算整個view的偏移量
        
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
        NSLog(@"textFieldDidBeginEditing");
        CGRect frame = textField.frame;
        CGFloat heights = self.view.frame.size.height;
// 當前點選textField的座標的Y值 + 當前點選textFiled的高度 - (螢幕高度- 鍵盤高度 - 鍵盤上tabbar高度)
// 在這一步就是這個當前textFiled的的最大Y值和鍵盤的最全高度的差值,用來計算整個view的偏移量
        int offset = frame.origin.y + 42- ( heights - 216.0-35.0);//鍵盤高度216
        NSTimeInterval animationDuration = 0.30f;
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        float width = self.view.frame.size.width;
        float height = self.view.frame.size.height;
        if(offset > 0)
        {
            CGRect rect = CGRectMake(0.0f, -offset,width,height);
            self.view.frame = rect;
        }
        [UIView commitAnimations];
}
    
 5. 點選空白處的時候讓其回到原來位置
        /**
        *  textField 取消選中狀態
        */
                    
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        NSLog(@"touchesBegan");
        [self.view endEditing:YES];
        NSTimeInterval animationDuration = 0.30f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
        self.view.frame = rect;
        [UIView commitAnimations];
}
                    
 6. 還有點選鍵盤的return鍵的時候恢復原狀就要在
                        
- (BOOL)textFieldShouldReturn:(UITextField *)textField;此方法裡面處理。
切記一定要判斷當前的textFiled是否是你點選的self.userNameText了。在讓他恢復原狀。
我是隻授之以漁的罌粟花,如果您有更好的使用方法歡迎留言交流!

相關文章