【问题标题】:Translate the view when the keyboard is opened (without scroll view)打开键盘时翻译视图(无滚动视图)
【发布时间】:2017-01-05 23:24:40
【问题描述】:

我有一个包含文本字段的 UIViewController。问题是当我创建 UIViewController 时我没有使用滚动视图。因此,当我使用下面的代码在打开键盘时翻译视图时,只有包含 textField 的 view1 被翻译。 view1 在主视图中,它具有约束:固定宽度和高度、垂直底部间距和带有超级视图的中心 X。

我是否可以在不使用 scrollView 的情况下翻译父根视图以便在显示键盘时显示文本字段?

这是我用来显示文本字段的代码(代码取自here)

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGFloat animatedDistance;
Inside textFieldShouldBeginEditing

 -(BOOL) textFieldShouldBeginEditing:(UITextField*)textField
 {    
     CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =  midline - viewRect.origin.y  - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
    * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }  
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
   if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown)
   {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
   }
   else
   {
       animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
   }
   CGRect viewFrame = self.view.frame;
   viewFrame.origin.y -= animatedDistance;

   [UIView beginAnimations:nil context:NULL];
   [UIView setAnimationBeginsFromCurrentState:YES];
   [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
   [self.view setFrame:viewFrame];
   [UIView commitAnimations];
   return YES;
}

- (BOOL) textFieldShouldEndEditing:(UITextField*)textField
{
     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y += animatedDistance;    
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationBeginsFromCurrentState:YES];
     [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];    
     [self.view setFrame:viewFrame];
     [UIView commitAnimations];
}

【问题讨论】:

标签: ios objective-c iphone


【解决方案1】:

您可以尝试使用下面的代码。如果您的视图延伸到键盘下方或上方,您必须调整代码以考虑到这一点。

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSLog(@"Keyboard showing");

    _keyboardSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect frame1 = _myScrollView.frame;
    float object1Y = _myScrollView.frame.size.height - _keyboardSize.height;
    frame1.origin.y = frame1.origin.y - object1Y;

    [UIView animateWithDuration:0.3f animations:^{
        _myScrollView.frame = frame1;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSLog(@"Keyboard hiding");

    _keyboardSize = CGSizeMake(0, 0);
    [UIView animateWithDuration:0.3f animations:^{
        // Reset the scroll view to the original position

    } completion:^(BOOL finished){

    }];
}

【讨论】:

  • 谢谢。它将与视图一起使用(没有滚动视图)?
  • 应该的。动画更改视图框架坐标。请注意,代码不完整。我不知道你的具体情况,所以你必须适应你的情况。
猜你喜欢
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多