Answer the question
In order to leave comments, you need to log in
Ios how to attach input field to keyboard?
I am writing an application for IOS, I added a view to the construction board and added a couple of elements, one of which is uitextfield. Everything is fine, I set up its processing, but one thing. When data begins to be entered into it, the appearing keyboard completely covers it and the person, as it were, blindly drives in data. How do I raise this field above the keyboard, ideally nail it to its top edge.
Answer the question
In order to leave comments, you need to log in
1. When the keyboard appears on the screen, the UIKeyboardWillShowNotification
notification "arrives" in the application . When the keyboard disappears from the screen - UIKeyboardWillHideNotification respectively.
In the controller, where you need to somehow respond to this behavior, you must subscribe to these notifications (for example, in viewDidLoad ):
- (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];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question