move up UIToolbar

前端 未结 5 1360
余生分开走
余生分开走 2020-12-08 12:16

I have a UIToolbar at bottom of my view, and I have a UITextField in this toolbar. When I begin editing this field, it is hidden behind the keyboard. To see what I\'ve typed

5条回答
  •  旧巷少年郎
    2020-12-08 12:41

    Building on the answers above and using the convenience method [UIView animateWithDuration...]. Observe the will show/hide keyboard notifications and use these handlers.

    - (void)keyboardWillShow:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
        NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
        NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey];
    
        [UIView animateWithDuration:durationValue.doubleValue
                              delay:0
                            options:(curveValue.intValue << 16)
                         animations:^{
                             self.navigationController.toolbar.frame = CGRectMake(0,
                                                                                  [endFrame CGRectValue].origin.y - self.navigationController.toolbar.bounds.size.height,
                                                                                  self.navigationController.toolbar.bounds.size.width,
                                                                                  self.navigationController.toolbar.bounds.size.height);
                         }
                         completion:nil];
    }
    
    - (void)keyboardWillHide:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
        NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
    
        [UIView animateWithDuration:durationValue.doubleValue
                              delay:0
                            options:(curveValue.intValue << 16)
                         animations:^{
                             self.navigationController.toolbar.frame = CGRectMake(0,
                                                                                  self.view.bounds.size.height - self.navigationController.toolbar.bounds.size.height,
                                                                                  self.navigationController.toolbar.bounds.size.width,
                                                                                  self.navigationController.toolbar.bounds.size.height);
                         }
                         completion:nil];
    }
    

提交回复
热议问题