iOS
- 8 ответов
- 0 вопросов
6
Вклад в тег
- (void) registerNotification {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) keyboardWillShow: (NSNotification *) notification {
NSValue *value = [notification.userInfo objectForKey: UIKeyboardFrameEndUserInfoKey];
CGRect keyEndFrame = [value CGRectValue];
CGRect keyFrame = [self.view convertRect:keyEndFrame toView:nil];
NSNumber *duration = [notification.userInfo objectForKey: UIKeyboardAnimationDurationUserInfoKey];
[self showKeyBoardWithKeyFrame:keyFrame andDuration:[duration doubleValue]];
}
- (void) showKeyBoardWithKeyFrame:(CGRect)keyFrame andDuration:(NSTimeInterval) duration {
BOOL animation = (duration > 0.0) ? YES : NO;
CGFloat keyboardHeight = keyFrame.size.height;
CGFloat scrollHeight = self.view.bounds.size.height - keyboardHeight;
CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
if (animation) {
[UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[_scrollView setFrame: scrollFrame];
} completion:nil];
} else {
[_scrollView setFrame: scrollFrame];
}
}