美文网首页
iOS 获取键盘高度

iOS 获取键盘高度

作者: 木马不在转 | 来源:发表于2016-07-26 11:45 被阅读35次

    - (void) registerForKeyboardNotifications{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];

    }

    - (void) keyboardWasShown:(NSNotification *) notif{

    NSDictionary *info = [notif userInfo];

    NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    CGRect scrollViewFrame= [scrollView frame];

    scrollViewFrame.size.height -= keyboardSize.height;

    scrollView.frame = scrollViewFrame;

    [scrollView scrollRectToVisible:inputElementFrame animated:YES];

    keyboardWasShown = YES;

    }

    - (void) keyboardWasHidden:(NSNotification *) notif{

    NSDictionary *info = [notif userInfo];

    NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];

    CGSize keyboardSize = [value CGRectValue].size;

    CGRect scrollViewFrame= [scrollView frame];

    scrollViewFrame.size.height += keyboardSize.height;

    scrollView.frame = scrollViewFrame;

    keyboardWasShown = NO;

    }

    相关文章

      网友评论

          本文标题:iOS 获取键盘高度

          本文链接:https://www.haomeiwen.com/subject/eyunjttx.html