文章目录
  1. 1. iOS开发中防止键盘挡住UITextField解决方案

iOS开发中防止键盘挡住UITextField解决方案

Ios-textField Ios技术文章收录

 ### iOS开发中防止键盘挡住UITextField解决方案

分类: iOS2012-09-04 14:30 560人阅读560人阅读 评论(1)评论(1) 收藏 举报
     最近转入ios开发,发现ios的UITextField如果在屏幕的最底部的时候,键盘不能自动的调整界面的布局,需要手动的调整位置才可以,所以自己研究和拿着笔话,想写一个通用的方法来实现每一个界面自动适配键盘的位置,这样的话,不用每一个界面去操作界面的位置了,具体的解决方案如下:

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

[plain] view plaincopy

  1. @interface BaseViewController : UIViewController 

  

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

  

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

代码:

[plain] view plaincopy

  1. IDNameField.delegate = self;

  

IDNameField是我继承BaseViewController的子类UIViewController中的一个UITextField,只要实现了上面的操作,我们的UITextField就可以在每一个界面实现自动适配调整界面,达到防止键盘挡住UITextField的效果了,以上出自Spring sky ,欢迎转载,请尊重作者的经验!如有疑问,请联系:

[IOS]textField被虚拟键盘挡住解决方法
     
经常会遇到以下情况,textField被虚拟键盘挡住的情况,解决。
RootViewController.h 中:

#import

@interface RootViewController : UIViewController {

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.

// 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 -

  • (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。
文章目录
  1. 1. iOS开发中防止键盘挡住UITextField解决方案