E
E
Evgeny Elchev2014-07-19 21:28:39
iOS
Evgeny Elchev, 2014-07-19 21:28:39

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 answer(s)
D
Dmitry Avvakumov, 2014-07-24
@rsi

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];
}

2. Consider a possible reaction to the appearance of the keyboard:
- (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]];
}

Along with the notification comes a lot of useful information about the behavior of the keyboard in the near future: its size, animation time, and even behavior during the animation (with fading or overclocking).
In the above code, we take the final frame ( keyEndFrame ) as an NSValue and convert it to the controller's coordinate system.
In addition, we get the duration of the animation ( duration ).
3. Reaction to the appearance
Let's assume that all our elements lie in a UIScrollView ( _scrollView ) and it occupies the entire screen area.
Then we just need to move its frame so that all elements have the opportunity to be shown on the screen.
- (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 question

Ask a Question

731 491 924 answers to any question