经常会遇到以下情况,textField被虚拟键盘挡住的情况,解决。
RootViewController.h 中:
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController<UITextFieldDelegate> {
UITextField *textField1;
UITextField *textField2;
}
@property (nonatomic,retain) UITextField *textField1;
@property (nonatomic ,retain) UITextField *textField2;
-(IBAction)backgroundTap:(id)sender;
@end
RootViewController.m 中:
#import "RootViewController.h"
@implementation RootViewController
@synthesize textField1;
@synthesize textField2;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
UIView *back = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
back.backgroundColor = [UIColor grayColor];
self.view = back;
[back release];
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 200, 30)];
textField1.backgroundColor = [UIColor clearColor];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.textColor = [UIColor redColor];
textField1.delegate = self;
[self.view addSubview:textField1];
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 200, 30)];
textField2.backgroundColor = [UIColor clearColor];
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.textColor = [UIColor redColor];
textField2.delegate = self;
[self.view addSubview:textField2];
}
#pragma mark -
#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti
{
//键盘输入的界面调整
//键盘的高度
float height = 216.0;
CGRect frame = self.view.frame;
frame.size = CGSizeMake(frame.size.width, frame.size.height - height);
[UIView beginAnimations:@"Curl"context:nil];//动画开始
[UIView setAnimationDuration:0.30];
[UIView setAnimationDelegate:self];
[self.view setFrame:frame];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
int offset = 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];
}
#pragma mark -
#pragma mark -
#pragma mark 触摸背景来关闭虚拟键盘
-(IBAction)backgroundTap:(id)sender
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
}
#pragma mark -
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[textField1 release];
[textField2 release];
[super dealloc];
}
RootViewController.m 中的backgroundTap:方法,用来实现触摸背景来关闭虚拟键盘。
这个方法用的时候首先把RootViewController上的view改成UIControl,然后通过UIControl的事件UIControlEventTouchDown来触发上面的方法backgroundTap: 。
注意下面的代码:
UIControl *_back = [[UIControl alloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColor grayColor];
self.view = _back;
[_back release];
[(UIControl *)self.view addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];
解决textField被键盘挡住的问题的方法有三个:
- (void)keyboardWillShow:(NSNotification *)noti;//调整虚拟键盘与self.view之间的关系。
-(BOOL)textFieldShouldReturn:(UITextField *)textField;//触摸键盘上的return键时关闭虚拟键盘
- (void)textFieldDidBeginEditing:(UITextField *)textField;//当编辑文本的时候,如果虚拟键盘挡住了textField,整个view就会向上移动。移动范围是一个键盘的高度216。