iPhone的软键盘为216像素高,程序员不得不在软键盘弹出时想办法调整窗口视图,避免输入控件被弹出的软键盘所遮挡。
iPhone程序员普遍采用注册软键盘事件的观察者对象来对付这个问题。当软键盘弹出时,窗体的View被上移,当软键盘隐藏时,窗体的View恢复原来位置,这样就避免了输入控件被遮挡。
KeyBoardUtil是我写的一个类,正是使用了上述原理,但进行了一些封装,便于程序员使用。
它的使用很简单。首先在要使用的ViewController中导入头文件:
#import"KeyBoardUtil.h"
并在类中声明一个KeyBoardUtil成员变量:
KeyBoardUtil* keyboardUtil;
在类的初始化方法中,初始化KeyBoardUtil对象:
keyboardUtil=[[KeyBoardUtil alloc]initWithOwner:self offsetY:100];
ower参数为self,offsetY参数则指定了键盘弹出时窗口View将要上移的像素,一般最高为软键盘全高216,但也可以不需要移动那么多,请根据实际情况决定需要移动多少。
然后在View Controller的显示和隐藏事件中分别调用KeyBoardUtil的reg和unreg方法:
#pragma mark 注册/注销键盘弹出通知方法
-(void)viewWillDisappear:(BOOL)animated
{
[keyboardUtil unreg];
[keyboardUtil release];
}
-(void)viewWillAppear:(BOOL)animated
{
[keyboardUtil reg];
}
全部代码就这些,是不是方便了许多?如果你喜欢这个小东西,那么可以去资源里下载:http://download.csdn.net/detail/kmyhy/3887363
更正:由于粗心大意,在源文件 KeyBoardUtil.m 的 keyboardWillShow: 方法中,误把“int height = owner.interfaceOrientation == UIInterfaceOrientationPortrait ? -offsetY+bug20px : -offsetY;”一行写成了"int height = owner.interfaceOrientation == UIInterfaceOrientationPortrait ? -offsetY+bug20px : offsetY;”。特此更正,在此向各位朋友致歉。
#import <Foundation/Foundation.h>
void freeKB(UIView* view);
@interface KeyBoardUtil : NSObject{
UIViewController* owner;
int offsetY;// 移动幅度
}
-(id)initWithOwner:(UIViewController*)o offsetY:(int)y;
-(void) reg;
-(void) unreg;
-(void)moveOwnerView : (CGFloat)height withDuration : (NSTimeInterval)animationDuration;
@end
//
// KeyBoardUtil.m
// mPortal
//
// Created by chen neng on 11-11-26.
// Copyright 2011 ydtf. All rights reserved.
//
#import "KeyBoardUtil.h"
void freeKB(UIView *view){//释放键盘方法
for (UIView *one in [view subviews]){
[one resignFirstResponder];
}
}
@implementation KeyBoardUtil
-(id)initWithOwner:(UIViewController *)o offsetY:(int)y{
self=[super init];
if (self) {
owner=o;
offsetY=y;
}
return self;
}
-(void) unreg{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
};
-(void) reg{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:owner.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:owner.view.window];
};
-(void) keyboardWillHide:(NSNotification *) notification
{
// 把self.view移回原点
NSDictionary* userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// 解决20像素Bug,正向:原点y坐标为0,倒向:原点y坐标下移20
int height = owner.interfaceOrientation == UIInterfaceOrientationPortrait ?20 : 0;
[self moveOwnerView:height withDuration:animationDuration];
}
-(void) keyboardWillShow:(NSNotification *) notification
{
// 把self.view上移
NSDictionary *userInfo = [notification userInfo];
// NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// CGRect keyboardRect = [aValue CGRectValue];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// 屏幕正向:上移(偏移坐标为负值),屏幕倒向:下移(偏移坐标为正值),解决20像素bug
int height = owner.interfaceOrientation == UIInterfaceOrientationPortrait ? -offsetY+20 : offsetY;
[self moveOwnerView:height withDuration:animationDuration];
}
#pragma mark 移动view
-(void)moveOwnerView : (CGFloat)height withDuration : (NSTimeInterval)animationDuration
{
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, height,owner.view.frame.size.width,owner.view.frame.size.height);
owner.view.frame = rect;
[UIView commitAnimations];
}
@end