textField被虚拟键盘挡住的情况

原文出处:http://blog.sina.com.cn/s/blog_702e40a80101d35e.html



经常会遇到以下情况,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.




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



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

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

3.在上面的代码中,我们已经增加了委托对UITextField的编辑监听,下面我们就要让我们的子类UIViewController去监听委托 代码:
[plain]  view plain copy
  1. IDNameField.delegate self;  
IDNameField是我继承BaseViewController的子类UIViewController中的一个UITextField,只要实现了上面的操作,我们的UITextField就可以在每一个界面实现自动适配调整界面,达到防止键盘挡住UITextField的效果了,
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值