textField被虚拟键盘挡住的3种解决方法

来源:互联网 发布:仙界网络直播间txt 编辑:程序博客网 时间:2024/05/21 13:42

经常会遇到以下情况,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 - 解决虚拟键盘挡住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 - 触摸背景来关闭虚拟键盘
-(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。
UITextField键盘自适应

最近转入ios开发,发现ios的UITextField如果在屏幕的最底部的时候,键盘不能自动的调整界面的布局,需要手动的调整位置才可以,所以自己研究和拿着笔话,想写一个通用的方法来实现每一个界面自动适配键盘的位置,这样的话,不用每一个界面去操作界面的位置了,具体的解决方案如下:

1. 先创建一个ViewController 继承自 UIViewController,让以后的每一个界面继承这个界面,在这个界面里面实现一个委托,代码如下:

[plain] view plaincopy
  1. @interface BaseViewController : UIViewController <UITextFieldDelegate>  
2.在这个BaseViewCOntroller.m文件中,现实UITextFieldDelegate中的两个方法,textFieldDidBeginEditing(开始编辑UITextField和 textFieldDidEndEditing(结束编辑UITextField),大家都知道,iphone的键盘都是固定的,都是系统自带的,没有第三方的输入法的,所以键盘的高度是固定的216,我们只要在开始编辑的时候,计算一下当前的UITextField的所在高度相对底部是否有216(就是UITextField的底部边缘相对屏幕的底部是否有216个长度),如果不够216,就需要把整体的view上移达到216高度即可;当我们结束编辑的时候,把之前增加的高度相反操作即可,代码如下:

//设置调整界面的动画效果//设置调整界面的动画效果

[plain] view plaincopy
  1. int prewTag ;  //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同  
  2. float prewMoveY; //编辑的时候移动的高度  
  3.   
  4. // 下面两个方法是为了防止TextFiled让键盘挡住的方法  
  5. /**  
  6.  开始编辑UITextField的方法  
  7.  */  
  8. -(void) textFieldDidBeginEditing:(UITextField *)textField  
  9. {  
  10.     CGRect textFrame =  textField.frame;  
  11.     float textY = textFrame.origin.y+textFrame.size.height;  
  12.     float bottomY = self.view.frame.size.height-textY;  
  13.     if(bottomY>=216)  //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度  
  14.     {  
  15.         prewTag = -1;  
  16.         return;  
  17.     }  
  18.     prewTag = textField.tag;  
  19.     float moveY = 216-bottomY;    
  20.     prewMoveY = moveY;  
  21.       
  22.     NSTimeInterval animationDuration = 0.30f;  
  23.     CGRect frame = self.view.frame;  
  24.     frame.origin.y -=moveY;//view的Y轴上移  
  25.     [UIView beginAnimations:@"ResizeView" context:nil];  
  26.     [UIView setAnimationDuration:animationDuration];  
  27.     self.view.frame = frame;  
  28.     [UIView commitAnimations];//设置调整界面的动画效果  
  29. }  
  30.   
  31. /**  
  32.  结束编辑UITextField的方法,让原来的界面还原高度  
  33.  */  
  34. -(void) textFieldDidEndEditing:(UITextField *)textField  
  35. {  
  36.     if(prewTag == -1) //当编辑的View不是需要移动的View  
  37.     {  
  38.         return;  
  39.     }  
  40.     float moveY ;  
  41.     NSTimeInterval animationDuration = 0.30f;  
  42.     CGRect frame = self.view.frame;  
  43.     if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动  
  44.     {   //还原界面  
  45.         moveY =  prewMoveY;  
  46.         frame.origin.y +=moveY;  
  47.     }  
  48.     //self.view移回原位置  
  49.     [UIView beginAnimations:@"ResizeView" context:nil];  
  50.     [UIView setAnimationDuration:animationDuration];  
  51.     self.view.frame = frame;  
  52.     [UIView commitAnimations];  
  53.     [textField resignFirstResponder];   
  54.   
  55.   
  56. }  

3.在上面的代码中,我们已经增加了委托对UITextField的编辑监听,下面我们就要让我们的子类UIViewController去监听委托

代码:

[plain] view plaincopy
  1. IDNameField.delegate = self;  
IDNameField是我继承BaseViewController的子类UIViewController中的一个UITextField,只要实现了上面的操作,我们的UITextField就可以在每一个界面实现自动适配调整界面,达到防止键盘挡住UITextField的效果了,
原创粉丝点击