textField被虛擬鍵盤擋住解決方法

[IOS]textField被虛擬鍵盤擋住解決方法

      

經常會遇到以下情況,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。



發佈了146 篇原創文章 · 獲贊 55 · 訪問量 136萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章