IOS鍵盤輸入屏幕上移

在IOS開法中經(jīng)常會(huì)遇到鍵盤遮擋屏幕的事情(比如輸入賬號(hào)密碼驗(yàn)證碼等等),就使得原本都不大的屏幕直接占了一半甚至更多的位置,這倒無所謂,關(guān)鍵是擋住了下面的按鈕。這樣的話按鈕的事件也就觸發(fā)不了,最好的解決辦法就是當(dāng)輸入這些信息的時(shí)候讓整個(gè)屏幕上移一個(gè)鍵盤的位置,或者上移到指定的位置。

因?yàn)椴皇侵挥械卿涀?cè)界面才會(huì)有這個(gè)問題,所以最好把這種聲明寫到基類里。然后所有的controller都繼承基類。

一、

首先一般輸入的話都用的是UITextField,所以要監(jiān)聽用戶什么時(shí)候開始輸入和什么時(shí)候結(jié)束輸入,直接設(shè)置代理代理就行了,要遵受

UITextFieldDelegate協(xié)議。

//遵循協(xié)議

@interfaceViewController()

//代理方法

#pragma mark - UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

return [textField resignFirstResponder];

}

//鍵盤彈出時(shí)屏幕上移

-(void)textFieldDidBeginEditing:(UITextField*)textField

{

//假如多個(gè)輸入,比如注冊(cè)和登錄,就可以根據(jù)不同的輸入框來上移不同的位置,從而更加人性化

//鍵盤高度216

//滑動(dòng)效果(動(dòng)畫)

CGRect frame = textField.frame;

int offset = frame.origin.y + 32 - (HEIGHT- 280.0);//鍵盤高度216

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

[UIView setAnimationDuration:animationDuration];

if(offset > 0)//加上屏幕上移textField是否會(huì)移除屏幕的判斷

{

CGRect rect = CGRectMake(0.0f, -offset,WIDTH,HEIGHT);

self.view.frame = rect;

}

[UIView commitAnimations];

}

//取消第一響應(yīng),也就是輸入完畢,屏幕恢復(fù)原狀

-(void)textFieldDidEndEditing:(UITextField*)textField

{

//滑動(dòng)效果

NSTimeIntervalanimationDuration =0.30f;

[UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];

[UIViewsetAnimationDuration:animationDuration];

//恢復(fù)屏幕

self.view.frame=CGRectMake(0.0f,0.0f,self.view.frame.size.width,self.view.frame.size.height);

[UIViewcommitAnimations];

}


二、

也可以用給鍵盤kvc實(shí)現(xiàn)功能

在基類的viewWillAppear方法中注冊(cè)監(jiān)聽。

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self setupForKeyBoard];

}

#pragma mark -鍵盤相關(guān)

//鍵盤的準(zhǔn)備

- (void)setupForKeyBoard

{

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

//點(diǎn)擊手勢(shì)

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyBoardHideAction)];

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];

//鍵盤顯示后添加手勢(shì)

[nc addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {

[self.view addGestureRecognizer:tap];

[self keyboardWillShow:note];

}];

//鍵盤消失后移除手勢(shì)

[nc addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification * _Nonnull note) {

[self.view removeGestureRecognizer:tap];

//鍵盤動(dòng)畫時(shí)間

double duration = [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

[UIView animateWithDuration:duration animations:^{

self.view.frame = CGRectMake(0,? 0 , WIDTH, HEIGHT);

}];

}];

}

//鍵盤消失后view下移

- (void)keyBoardHideAction

{

[self.view endEditing:YES];

}

//通過note獲取鍵盤高度,鍵盤動(dòng)畫時(shí)間

- (void)keyboardWillShow:(NSNotification *)note

{

//獲取鍵盤高度,在不同設(shè)備上,以及中英文下是不同的

CGFloat keyboardH = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

//計(jì)算出鍵盤頂端到inputTextView panel底端的距離(加上自定義的緩沖距離INTERVAL_KEYBOARD)

CGFloat offset = (self.viewBottom+INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight);

//鍵盤動(dòng)畫時(shí)間

double duration = [[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

if (offset > 0){

[UIView animateWithDuration:duration animations:^{

self.view.frame = CGRectMake(0, - offset ), WIDTH, HEIGHT);

}];


}



#pragma mark - UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView

{

//將textField的rect轉(zhuǎn)換到self.view上

CGRect rect = [textView.superview convertRect:textView.frame toView:self.view];

//textField的底部

self.viewBottom = rect.origin.y + rect.size.height;

}

- (void)textViewDidEndEditing:(UITextView *)textView

{

[UIView animateWithDuration:0.25 animations:^{

self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);

}];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容