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

Posted

技术标签:

【中文标题】打开键盘时翻译视图(无滚动视图)【英文标题】: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];

【问题讨论】:

为什么将所有这些键盘常量定义为宏?使用适当的键盘通知在运行时获取它们。 你能举个例子吗?没有scrollView是否可以让它工作? 搜索UIKeyboardWillShowNotification UIKeyboardFrameEndUserInfoKey UIKeyboardAnimationDurationUserInfoKey 好的,我会的。谢谢 apeth.com/iosBook/ch23.html#_keyboard_covers_text_field 【参考方案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)

    ];

【讨论】:

谢谢。它将与视图一起使用(没有滚动视图)? 应该的。动画更改视图框架坐标。请注意,代码不完整。我不知道你的具体情况,所以你必须适应你的情况。

以上是关于打开键盘时翻译视图(无滚动视图)的主要内容,如果未能解决你的问题,请参考以下文章

我设置滚动视图偏移以显示被键盘隐藏的文本字段。如果用户在显示键盘时滚动,则滚动视图会向下捕捉

滚动 UIScrollView 以适应 iOS 中的键盘

键盘出现多次时,滚动视图不会向上移动

目标 C:表格视图滚动仅在键盘打开时有效

当视图控制器内部有滚动视图时处理键盘事件

滚动到滚动视图/滚动视图中的特定文本字段 当键盘在屏幕上时停止滚动