您的位置:首页 > 移动开发 > IOS开发

iOS——获取键盘高度,

2016-10-17 18:14 190 查看
- (void)viewDidLoad

{

    [super viewDidLoad];

    //使用NSNotificationCenter 键盘出现时

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWasShown:)

                                                 name:UIKeyboardWillShowNotification object:nil];

    

    //使用NSNotificationCenter 键盘隐藏时

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillBeHidden:)

                                                 name:UIKeyboardWillHideNotification object:nil];

    

    [self reloadBusLine];
}

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)keyboardWillShow:(NSNotification *)notification
{

NSDictionary *userInfo = [notification userInfo];

NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;

NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];

textView.frame = newTextViewFrame;

[UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification
{

NSDictionary* userInfo = [notification userInfo];

NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];

textView.frame = self.view.bounds;

[UIView commitAnimations];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: