美文网首页iOS专题
iOS 解决文本框被键盘弹出挡住的问题

iOS 解决文本框被键盘弹出挡住的问题

作者: Zhen斌iOS | 来源:发表于2020-06-09 14:07 被阅读0次

    解决文本框被键盘弹出挡住的问题,如下代码:

    -(void)touchesBegan:(NSSet* )toucheswithEvent:(UIEvent *)event{
        [username_text resignFirstResponder];
        [password_text resignFirstResponder];
        // When the user presses return,take focus away from the textfield so that the keyboard is dismissed.
        NSTimeIntervalanimationDuration = 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];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField* )textField {
      // When the user presses return, take focus away from the textfield so that the keyboard is dismissed.
        NSTimeIntervalanimationDuration=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];
        [textField resignFirstResponder]; 
        returnYES;
    }
    
    - (void)textFieldDidBeginEditing:(UITextField* )textField { 
        CGRectframe=password_text.frame;
        intoffset=frame.origin.y+32-(self.view.frame.size.height-216.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];
    }
     
    

    希望对你有帮助!

    相关文章

      网友评论

        本文标题:iOS 解决文本框被键盘弹出挡住的问题

        本文链接:https://www.haomeiwen.com/subject/rjmstktx.html