声明一个键盘高度属性
@property (nonatomic, assign)CGSize kbSize;
在 viewDidLoad 中视图注册通知中心
[self registerForKeyboardNotifications];
pragma mark -- 注册键盘的通知中心获取键盘的高度
//注册一个键盘的通知中心
- (void)registerForKeyboardNotifications{
//使用NSNotificationCenter 键盘出现时
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
}
//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
//kbSize键盘尺寸 (有width, height)
self.kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//键盘高度
NSLog(@"hight_hitht:%f",self.kbSize.height);
//self.view.frame = self.view.frame.size.height - self.kbSize.height;
CGRect currentFrame = self.view.frame;
CGFloat change =self.kbSize.height;
currentFrame.origin.y = currentFrame.origin.y - change ;
[UIView animateWithDuration:0.00000000001 animations:^{
self.view.frame = currentFrame;
}];
}`
// 视图将要消失时,移除通知中心
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}