你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS――UITextField被鍵盤遮蔽解決方案

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

編輯:關於IOS



本人在做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];//在視圖控制器消除時,移除鍵盤事件的通知

}

希望對大家有幫助!



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved