解决文本框被键盘弹出挡住的问题,如下代码:
-(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];
}
希望对你有帮助!
网友评论