IOS——UITextField被鍵盤遮蔽解決方案

來源:互聯網
上載者:User



本人在做ios開發的時候碰到一個老生常談的問題,UITextField被鍵盤遮蔽問題,網上搜尋了一些資料,找到了兩種前輩寫的方案,方案一:http://blog.csdn.net/springsky_/article/details/7941858,在ios5之前適用,但是在5之後盤布局變了,尤其是中文輸入時,中文漢字選擇框就固定在鍵盤上方,於是有前輩出了第二種方案:http://www.apkbus.com/home.php?mod=space&uid=107838&do=blog&id=44715。第二種方案是直接把整個view向上移動鍵盤的寬度,有些情境顯得不是很恰當,本人在兩位的基礎上做了些許改動,具體步驟如下:

本人定義了一個基本的ViewController——BaseViewController。

.h檔案內容如下:

#import

@interface BaseViewController :UIViewController{

UITextField *_checkText; //用來標識哪一個UITextField被點擊

@property(nonatomic) UITextField*checkText;

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeightwithDuration:(NSTimeInterval)_NSTimeInterval;

@end

.m檔案內容如下:

#import "BaseViewController.h"

@implementation BaseViewController

-(void)viewDidLoad{

[super viewDidLoad];

//註冊通知

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

//鍵盤高度變化通知,ios5.0新增的

#ifdef __IPHONE_5_0

float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 5.0) {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillChangeFrameNotification object:nil];

}

#endif

}

#pragma mark開始編輯UITextField,本人試過這個方法在keyboardWillShow之前被調用

-(void)textFieldDidBeginEditing:(UITextField*)textField{

_checkText = textField;//設定被點擊的對象

}

#pragma mark -鍵盤彈出時調用的方法

#pragma mark Responding to keyboard events

- (void)keyboardWillShow:(NSNotification*)notification {

if (nil == _checkText) {

return;

}

/*

Reduce the size of the text view so that it's not obscured by thekeyboard.

Animate the resize so that it's in sync with the appearance of thekeyboard.

*/

NSDictionary *userInfo = [notification userInfo];

// Get the origin of the keyboard when it's displayed.

NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

// Get the top of the keyboard as the y coordinate of its origin inself's view's coordinate system. The bottom of the text view's frame shouldalign with the top of the keyboard's final position.

CGRect keyboardRect = [aValue CGRectValue];

// Get the duration of the animation.

NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

NSTimeInterval animationDuration;

[animationDurationValue getValue:&animationDuration];

CGRect textFrame = _checkText.frame;//當前UITextField的位置

float textY = textFrame.origin.y + textFrame.size.height;//得到UITextField下邊框距離頂部的高度

float bottomY = self.view.frame.size.height - textY;//得到下邊框到底部的距離

if(bottomY >=keyboardRect.size.height ){//鍵盤預設高度,如果大於此高度,則直接返回

return;

}

float moveY = keyboardRect.size.height - bottomY;

// Animate the resize of the text view's frame in sync with the keyboard'sappearance.

[self moveInputBarWithKeyboardHeight:moveYwithDuration:animationDuration];

}

//鍵盤被隱藏的時候調用的方法

(void)keyboardWillHide:(NSNotification*)notification {

NSDictionary* userInfo = [notification userInfo];

/*

Restore the size of the text view (fill self's view).

Animate the resize so that it's in sync with the disappearance of thekeyboard.

*/

NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

NSTimeInterval animationDuration;

[animationDurationValue getValue:&animationDuration];

[self moveInputBarWithKeyboardHeight:0.0withDuration:animationDuration];

}

#pragma mark移動view

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeightwithDuration:(NSTimeInterval)_NSTimeInterval{

CGRect rect = self.view.frame;

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:_NSTimeInterval];

rect.origin.y = -_CGRectHeight;//view往上移動

self.view.frame = rect;

[UIView commitAnimations];

}

-(void)dealloc{

[[NSNotificationCenter defaultCenter] removeObserver:self];//在視圖控制器消除時,移除鍵盤事件的通知

}

希望對大家有協助!



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.