天天看點

轉 iOS擷取軟鍵盤的高度

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //增加監聽,當鍵盤出現或改變時收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加監聽,當鍵退出時收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    
}

//當鍵盤出現或改變時調用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //擷取鍵盤的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
}

//當鍵退出時調用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    
}      

轉 http://blog.sina.com.cn/s/blog_9693f61a01017ffk.html

繼續閱讀