在学习编程的过程中,我觉得不止要获得课本的知识,更多的是解决问题的方法,面对新问题该怎么解决,这样我们才能走在最前方,我是达内的学员,感谢你对本博客的支持;
最近转入ios开发,发现ios的UITextField如果在屏幕的最底部的时候,键盘不能自动的调整界面的布局,需要手动的调整位置才可以,所以自己研究和拿着笔话,想写一个通用的方法来实现每一个界面自动适配键盘的位置,这样的话,不用每一个界面去操作界面的位置了,具体的解决方案如下:
1. 先创建一个UIViewController 这个UIViewController作为父类,让以后的每一个界面继承这个界面,在这个界面里面实现一个委托,代码如下:
[plain]
@interface BaseViewController : UIViewController
@interface BaseViewController : UIViewController
2.在这个BaseViewCOntroller.m文件中,现实UITextFieldDelegate中的两个方法,textFieldDidBeginEditing(开始编辑UITextField和 textFieldDidEndEditing(结束编辑UITextField),大家都知道,iphone的键盘都是固定的,都是系统自带的,没有第三方的输入法的,所以键盘的高度是固定的216,我们只要在开始编辑的时候,计算一下当前的UITextField的所在高度相对底部是否有216(就是UITextField的底部边缘相对屏幕的底部是否有216个长度),如果不够216,就需要把整体的view上移达到216高度即可;当我们结束编辑的时候,把之前增加的高度相反操作即可,代码如下:
//设置调整界面的动画效果//设置调整界面的动画效果
[plain]
int prewTag ; //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同
float prewMoveY; //编辑的时候移动的高度
// 下面两个方法是为了防止TextFiled让键盘挡住的方法
/**
开始编辑UITextField的方法
*/
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFrame = textField.frame;
float textY = textFrame.origin.y+textFrame.size.height;
float bottomY = self.view.frame.size.height-textY;
if(bottomY>=216) //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度
{
prewTag = -1;
return;
}
prewTag = textField.tag;
float moveY = 216-bottomY;
prewMoveY = moveY;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
frame.origin.y -=moveY;//view的Y轴上移
frame.size.height +=moveY; //View的高度增加
self.view.frame = frame;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];//设置调整界面的动画效果
}
/**
结束编辑UITextField的方法,让原来的界面还原高度
*/
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if(prewTag == -1) //当编辑的View不是需要移动的View
{
return;
}
float moveY ;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
{ //还原界面
moveY = prewMoveY;
frame.origin.y +=moveY;
frame.size. height -=moveY;
self.view.frame = frame;
}
//self.view移回原位置
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
[textField resignFirstResponder];
}
int prewTag ; //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同
float prewMoveY; //编辑的时候移动的高度
// 下面两个方法是为了防止TextFiled让键盘挡住的方法
/**
开始编辑UITextField的方法
*/
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFrame = textField.frame;
float textY = textFrame.origin.y+textFrame.size.height;
float bottomY = self.view.frame.size.height-textY;
if(bottomY>=216) //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度
{
prewTag = -1;
return;
}
prewTag = textField.tag;
float moveY = 216-bottomY;
prewMoveY = moveY;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
frame.origin.y -=moveY;//view的Y轴上移
frame.size.height +=moveY; //View的高度增加
self.view.frame = frame;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];//设置调整界面的动画效果
}
/**
结束编辑UITextField的方法,让原来的界面还原高度
*/
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if(prewTag == -1) //当编辑的View不是需要移动的View
{
return;
}
float moveY ;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
{ //还原界面
moveY = prewMoveY;
frame.origin.y +=moveY;
frame.size. height -=moveY;
self.view.frame = frame;
}
//self.view移回原位置
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
[textField resignFirstResponder];
}
3.在上面的代码中,我们已经增加了委托对UITextField的编辑监听,下面我们就要让我们的子类UIViewController去监听委托
代码:
[plain]
IDNameField.delegate = self;
IDNameField.delegate = self;IDNameField是我继承BaseViewController的子类UIViewController中的一个UITextField,只要实现了上面的操作,我们的UITextField就可以在每一个界面实现自动适配调整界面,达到防止键盘挡住UITextField的效果了。
知识就是力量,别人学用知识来赚钱的成本低,在达内学习到编程是铸造梦想的地方,如果你喜欢编程,这里,有我们共同语言,共同的故事,共同的梦想!
(责任编辑:广州达内培训转载于:http://tarena3g.com.cn)
最近转入ios开发,发现ios的UITextField如果在屏幕的最底部的时候,键盘不能自动的调整界面的布局,需要手动的调整位置才可以,所以自己研究和拿着笔话,想写一个通用的方法来实现每一个界面自动适配键盘的位置,这样的话,不用每一个界面去操作界面的位置了,具体的解决方案如下:
1. 先创建一个UIViewController 这个UIViewController作为父类,让以后的每一个界面继承这个界面,在这个界面里面实现一个委托,代码如下:
[plain]
@interface BaseViewController : UIViewController
@interface BaseViewController : UIViewController
2.在这个BaseViewCOntroller.m文件中,现实UITextFieldDelegate中的两个方法,textFieldDidBeginEditing(开始编辑UITextField和 textFieldDidEndEditing(结束编辑UITextField),大家都知道,iphone的键盘都是固定的,都是系统自带的,没有第三方的输入法的,所以键盘的高度是固定的216,我们只要在开始编辑的时候,计算一下当前的UITextField的所在高度相对底部是否有216(就是UITextField的底部边缘相对屏幕的底部是否有216个长度),如果不够216,就需要把整体的view上移达到216高度即可;当我们结束编辑的时候,把之前增加的高度相反操作即可,代码如下:
//设置调整界面的动画效果//设置调整界面的动画效果
[plain]
int prewTag ; //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同
float prewMoveY; //编辑的时候移动的高度
// 下面两个方法是为了防止TextFiled让键盘挡住的方法
/**
开始编辑UITextField的方法
*/
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFrame = textField.frame;
float textY = textFrame.origin.y+textFrame.size.height;
float bottomY = self.view.frame.size.height-textY;
if(bottomY>=216) //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度
{
prewTag = -1;
return;
}
prewTag = textField.tag;
float moveY = 216-bottomY;
prewMoveY = moveY;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
frame.origin.y -=moveY;//view的Y轴上移
frame.size.height +=moveY; //View的高度增加
self.view.frame = frame;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];//设置调整界面的动画效果
}
/**
结束编辑UITextField的方法,让原来的界面还原高度
*/
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if(prewTag == -1) //当编辑的View不是需要移动的View
{
return;
}
float moveY ;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
{ //还原界面
moveY = prewMoveY;
frame.origin.y +=moveY;
frame.size. height -=moveY;
self.view.frame = frame;
}
//self.view移回原位置
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
[textField resignFirstResponder];
}
int prewTag ; //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同
float prewMoveY; //编辑的时候移动的高度
// 下面两个方法是为了防止TextFiled让键盘挡住的方法
/**
开始编辑UITextField的方法
*/
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFrame = textField.frame;
float textY = textFrame.origin.y+textFrame.size.height;
float bottomY = self.view.frame.size.height-textY;
if(bottomY>=216) //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度
{
prewTag = -1;
return;
}
prewTag = textField.tag;
float moveY = 216-bottomY;
prewMoveY = moveY;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
frame.origin.y -=moveY;//view的Y轴上移
frame.size.height +=moveY; //View的高度增加
self.view.frame = frame;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];//设置调整界面的动画效果
}
/**
结束编辑UITextField的方法,让原来的界面还原高度
*/
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if(prewTag == -1) //当编辑的View不是需要移动的View
{
return;
}
float moveY ;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
{ //还原界面
moveY = prewMoveY;
frame.origin.y +=moveY;
frame.size. height -=moveY;
self.view.frame = frame;
}
//self.view移回原位置
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
[textField resignFirstResponder];
}
3.在上面的代码中,我们已经增加了委托对UITextField的编辑监听,下面我们就要让我们的子类UIViewController去监听委托
代码:
[plain]
IDNameField.delegate = self;
IDNameField.delegate = self;IDNameField是我继承BaseViewController的子类UIViewController中的一个UITextField,只要实现了上面的操作,我们的UITextField就可以在每一个界面实现自动适配调整界面,达到防止键盘挡住UITextField的效果了。
知识就是力量,别人学用知识来赚钱的成本低,在达内学习到编程是铸造梦想的地方,如果你喜欢编程,这里,有我们共同语言,共同的故事,共同的梦想!
(责任编辑:广州达内培训转载于:http://tarena3g.com.cn)