課程咨(zi)詢: 400-996-5531 / 投訴建議: 400-111-8989
認真做(zuo)教(jiao)育 專心(xin)促就業
最近轉入(ru)ios開發(fa),發(fa)現(xian)ios的(de)(de)(de)UITextField如(ru)果在屏(ping)幕的(de)(de)(de)最底部的(de)(de)(de)時候(hou),鍵盤不能自動(dong)的(de)(de)(de)調整(zheng)界(jie)面的(de)(de)(de)布局,需(xu)要手動(dong)的(de)(de)(de)調整(zheng)位置才可以,所以自己研究和拿著(zhu)筆話(hua),想寫一(yi)個通用的(de)(de)(de)方法來(lai)實現(xian)每(mei)一(yi)個界(jie)面自動(dong)適(shi)配(pei)鍵盤的(de)(de)(de)位置,這樣的(de)(de)(de)話(hua),不用每(mei)一(yi)個界(jie)面去操作界(jie)面的(de)(de)(de)位置了,具體的(de)(de)(de)解(jie)決方案如(ru)下:
1. 先創建一(yi)個(ge)UIViewController 這(zhe)(zhe)(zhe)個(ge)UIViewController作為父(fu)類,讓(rang)以后的每一(yi)個(ge)界面(mian)繼承這(zhe)(zhe)(zhe)個(ge)界面(mian),在這(zhe)(zhe)(zhe)個(ge)界面(mian)里面(mian)實(shi)現(xian)一(yi)個(ge)委托,代(dai)碼如下:
[plain]
@interface BaseViewController : UIViewController
@interface BaseViewController : UIViewController
2.在這(zhe)個BaseViewCOntroller.m文件(jian)中(zhong),現實UITextFieldDelegate中(zhong)的(de)(de)兩個方(fang)法,textFieldDidBeginEditing(開始編輯UITextField和 textFieldDidEndEditing(結束(shu)編輯UITextField),大(da)家都知道,iphone的(de)(de)鍵盤(pan)都是固定的(de)(de),都是系統(tong)自(zi)帶的(de)(de),沒(mei)有第三方(fang)的(de)(de)輸(shu)入(ru)法的(de)(de),所以鍵盤(pan)的(de)(de)高(gao)(gao)度(du)(du)(du)是固定的(de)(de)216,我們(men)只要在開始編輯的(de)(de)時(shi)候(hou),計(ji)算一下當前的(de)(de)UITextField的(de)(de)所在高(gao)(gao)度(du)(du)(du)相對底(di)部(bu)是否(fou)有216(就是UITextField的(de)(de)底(di)部(bu)邊(bian)緣(yuan)相對屏幕(mu)的(de)(de)底(di)部(bu)是否(fou)有216個長度(du)(du)(du)),如果不夠(gou)216,就需(xu)要把(ba)整體的(de)(de)view上(shang)移達到216高(gao)(gao)度(du)(du)(du)即可(ke);當我們(men)結束(shu)編輯的(de)(de)時(shi)候(hou),把(ba)之前增加的(de)(de)高(gao)(gao)度(du)(du)(du)相反操(cao)作即可(ke),代碼如下:
//設置(zhi)調(diao)整界面的(de)動畫(hua)效(xiao)果//設置(zhi)調(diao)整界面的(de)動畫(hua)效(xiao)果
[plain]
int prewTag ; //編輯上(shang)一個UITextField的TAG,需要在XIB文(wen)件(jian)中定義或者程(cheng)序中添加,不(bu)能讓兩個控(kong)件(jian)的TAG相同
float prewMoveY; //編輯的(de)時候(hou)移(yi)動的(de)高度(du)
// 下(xia)面(mian)兩(liang)個方法是為了防止TextFiled讓(rang)鍵盤擋住(zhu)的方法
/**
開始編輯UITextField的方(fang)法
*/
-(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) //判(pan)斷當前的高(gao)度是否已經有216,如果超過了就不需要再移動主界面的View高(gao)度
{
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軸上(shang)移
frame.size.height +=moveY; //View的高度(du)增加
self.view.frame = frame;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];//設置調整界面的動畫效果
}
/**
結束(shu)編輯UITextField的方法,讓原(yuan)來的界(jie)面還(huan)原(yuan)高度
*/
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if(prewTag == -1) //當(dang)編輯的(de)(de)View不是需(xu)要(yao)移動的(de)(de)View
{
return;
}
float moveY ;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
if(prewTag == textField.tag) //當結束(shu)編輯(ji)的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 ; //編輯上(shang)一個UITextField的(de)TAG,需要(yao)在XIB文件(jian)中定義或者程序中添加,不能讓兩個控件(jian)的(de)TAG相同
float prewMoveY; //編輯的(de)時(shi)候移動的(de)高度
// 下面兩個方法(fa)是為了防止TextFiled讓鍵盤擋(dang)住的方法(fa)
/**
開始編(bian)輯(ji)UITextField的方法(fa)
*/
-(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) //判斷(duan)當前(qian)的高(gao)(gao)度是否已經有216,如(ru)果超過了就不需(xu)要再移動(dong)主界面的View高(gao)(gao)度
{
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軸(zhou)上移
frame.size.height +=moveY; //View的高(gao)度增(zeng)加
self.view.frame = frame;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];//設置調整界面的(de)動畫效(xiao)果
}
/**
結束(shu)編輯UITextField的(de)方法,讓原來的(de)界(jie)面(mian)還原高度
*/
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if(prewTag == -1) //當(dang)編輯的View不是需要移動的View
{
return;
}
float moveY ;
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
if(prewTag == textField.tag) //當結束編輯的View的TAG是上次的就(jiu)移動
{ //還(huan)原界面
moveY = prewMoveY;
frame.origin.y +=moveY;
frame.size. height -=moveY;
self.view.frame = frame;
}
//self.view移回(hui)原位置
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
[textField resignFirstResponder];
}
3.在(zai)上面(mian)的代碼中,我(wo)(wo)們(men)已經(jing)增(zeng)加了(le)委托(tuo)對UITextField的編(bian)輯監聽,下面(mian)我(wo)(wo)們(men)就要讓(rang)我(wo)(wo)們(men)的子類UIViewController去(qu)監聽委托(tuo)
代碼:
[plain]
IDNameField.delegate = self;
IDNameField.delegate = self;IDNameField是我繼承BaseViewController的(de)(de)(de)子類UIViewController中的(de)(de)(de)一個(ge)UITextField,只要實現了上面(mian)的(de)(de)(de)操作,我們的(de)(de)(de)UITextField就可以(yi)在每一個(ge)界面(mian)實現自(zi)動適配(pei)調整界面(mian),達(da)到防止鍵盤擋住UITextField的(de)(de)(de)效果(guo)了。
【免責聲明】本文(wen)部分系轉(zhuan)載,轉(zhuan)載目的(de)在于(yu)傳遞更多(duo)信(xin)息,并不代表本網贊同其(qi)觀點(dian)和對其(qi)真(zhen)實(shi)性(xing)負(fu)責。如涉及(ji)作品(pin)內(nei)容(rong)、版權和其(qi)它問題,請(qing)在30日內(nei)與聯系我們,我們會予以(yi)更改或刪除相(xiang)關文(wen)章,以(yi)保證您的(de)權益(yi)!