将元素定位到视图的底部

时间:2011-01-17 15:45:13

标签: iphone cocoa-touch

SHORT:

当视图显示在屏幕上时,它的高度为460点。当我向上滑动以显示键盘时,它的高度为480点。

LONG:

我有一个视图,当键盘出现时会向上滑动。

我使用以下代码计算与视图底部对齐的输入的新位置:

self.view.bounds.size.height + self.view.frame.origin.y - inputHeight

当键盘未显示时,似乎self.view.bounds.size.height 460 点且self.view.frame.origin.y 0 。这个声音正确,因为状态栏占据了屏幕顶部的20点。

当显示键盘并且视图向上滑216点(键盘高度)时,我得到self.view.bounds.size.height 676 self.view.frame.origin.y -196 ,加起来 480

我需要一种更好的方法将输入对齐到屏幕底部。

1 个答案:

答案 0 :(得分:1)

这个视图背后的想法是将它连接到默认系统键盘的顶部以扩展基本输入功能吗?如果是这种情况,听起来就是这样,那么内置的解决方案就更简单了。从UIResponder继承的任何类(任何触发键盘外观的类)都可以声明Input Accessory View,它将自动保持附加到默认键盘的顶部。

编辑:好的,这不是你的情况。在这种情况下,我将分享一个我在一些应用程序中使用的方法来手动翻译键盘的外观。首先,我更喜欢从系统中获取转换的细节,而不是像216这样的硬编码值。这使得它很好并且面向未来,在不太可能的情况下,键盘的大小会被Apple改变。此信息在NSNotification中可用。要获得通知,请在以下位置注册:

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

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

然后,在处理程序中:

- (void)_keyboardWillShow:(NSNotification *)notification {
    [self _animateWithKeyboardNotification:notification];
}

- (void)_keyboardWillHide:(NSNotification *)notification {
    [self _animateWithKeyboardNotification:notification];
}

并且,对于主要行为:

- (void)_animateWithKeyboardNotification:(NSNotification*)keyboardNotification {
    /*   Animates a view with an animation constructed from the system keyboard
     *   animation properties.  The animation is a vertical translation to compensate
     *   for the area now covered by the keyboard.
     */

    UIViewAnimationCurve curve = [[keyboardNotification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
    NSTimeInterval duration = [[keyboardNotification.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat keyboardHeight = [[keyboardNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

    // Set the transform based on the keyboard action: Translate up if showing, reset to identity when hiding
    CGAffineTransform animationTransform;
    if ([keyboardNotification.name isEqualToString:UIKeyboardWillShowNotification]) 
        animationTransform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
    } else if ([keyboardNotification.name isEqualToString:UIKeyboardWillHideNotification]) {
        animationTransform = CGAffineTransformIdentity;
    }

    // Apply the animation.
    [UIView animateWithDuration:duration delay:0.0f options:curve animations:^ {
        self.view.transform = animationTransform;
    } completion:^ (BOOL finished) {
        // Completion handler, if needed.   
    }];
}

现在,有了这个确切的方法,我正在翻译整个视图。您可以调整它以仅在需要时翻译输入视图,或进行任何其他小调整。我喜欢这种耐用性的方法。