欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

注册键盘通知,动态读取键盘的高度

程序员文章站 2022-05-16 09:09:07
...

动态读取键盘的高度

 

 

 

- (void) registerForKeyboardNotifications

{

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotificationobject:nil];

    

    [[NSNotificationCenterdefaultCenteraddObserver:selfselector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotificationobject:nil];

    

    [[NSNotificationCenterdefaultCenteraddObserver:selfselector:@selector(keyChangeFrame:) name:UIKeyboardWillChangeFrameNotificationobject:nil];

}

 

- (void) keyboardWasShown:(NSNotification *) notif

{

    NSDictionary *info = [notif userInfo];

    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    

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

    self.myLabel.frame = CGRectMake(0, APPBOUND.size.height-keyboardSize.height-44-35, APPBOUND.size.width, 35);

    ///keyboardWasShown = YES;

}

- (void) keyboardWasHidden:(NSNotification *) notif

{

    NSDictionary *info = [notif userInfo];

    

    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

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

    // keyboardWasShown = NO;

    

}

 

- (void) keyChangeFrame:(NSNotification *) notif

{

    NSDictionary *info = [notif userInfo];

    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    

    SJBLog(@"keyBoard:%f,,,info==%@", keyboardSize.height,info);  //216

    self.myLabel.frame = CGRectMake(0, APPBOUND.size.height-keyboardSize.height-44-35, APPBOUND.size.width, 35);

    ///keyboardWasShown = YES;

 

}