---
title: "监控键盘的高度"
author: "刺骨的夜"
date: "2015-01-12T16:05:22Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2015-01-12T17:12:22Z"
id: 2175073
url: "https://lanxs.iteye.com/blog/2175073"
---

\- (id)initWithFrame:(CGRect)frame

{

    self = \[super initWithFrame:frame];

    if (self) {

        \[self registerForKeyboardNotifications];

     }

    return self;

 

}

 

\- (void) registerForKeyboardNotifications

{

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

    \[\[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardWillhiden:)name: UIKeyboardWillHideNotification object:nil];

}

 

\- (void) keyboardWillShow:(NSNotification \*) notif

{

    

    

    NSDictionary \*info = \[notif userInfo];

    NSValue \*value = \[info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = \[value CGRectValue].size;

    

    NSLog(@"keyBoard:%f", keyboardSize.height);  //216

    ///keyboardWasShown = YES;

    

    self.frame =  CGRectMake(0, self.reply\_heigth - keyboardSize.height - self.frame.size.height, SCREEN\_WITH, 40);

}

\- (void) keyboardWillhiden:(NSNotification \*) notif

{

    

    NSDictionary \*info = \[notif userInfo];

    

    NSValue \*value = \[info objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    CGSize keyboardSize = \[value CGRectValue].size;

    NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.height);

    // keyboardWasShown = NO;

    [UIView animateWithDuration:0.15 animations:^{

        self.frame =  CGRectMake(0, self.reply\_heigth - keyboardSize.height - self.frame.size.height, SCREEN\_WITH, 40);

    }];

 

}