键盘打开时翻译视图(无滚动视图)

时间:2017-01-05 23:24:41

标签: ios objective-c iphone

我有一个包含textFields的UIViewController。问题是,当我创建UIViewController时,我没有使用滚动视图。因此,当我在打开键盘时使用下面的代码翻译视图时,只翻译包含textField的view1。 view1位于主视图中,它具有以下限制:固定宽度和高度,垂直底部间距和带有超视图的中心X.

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

这是我用来显示textField的代码(代码取自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];
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码。如果您的视图扩展到键盘下方或上方,则必须调整代码。

- (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){

    }];
}