iOS专题

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

2020-06-09  本文已影响0人  Zhen斌iOS

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

-(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];
}
 

希望对你有帮助!

上一篇 下一篇

猜你喜欢

热点阅读