热门搜索 :
考研考公
您的当前位置:首页正文

iOS 获取键盘高度

来源:东饰资讯网

- (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;

}

Top