Iphone 将图元定位到视图的底部

Iphone 将图元定位到视图的底部,iphone,cocoa-touch,Iphone,Cocoa Touch,简称: 当视图显示在屏幕上时,其高度为460点。当我向上滑动它以显示键盘时,它的高度为480点 长: 当键盘出现时,我有一个向上滑动的视图 我使用以下代码计算与视图底部对齐的输入的新位置: self.view.bounds.size.height + self.view.frame.origin.y - inputHeight 当键盘未显示时,self.view.bounds.size.height似乎是460点,self.view.frame.origin.y是0。这听起来不错,因为状态栏占

简称:

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

长:

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

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

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


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

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

编辑:好吧,那不是你的情况。在这种情况下,我将分享我在一些应用程序中使用的一种方法,用键盘的外观手动翻译各种东西。首先,我更喜欢从系统获得转换的细节,而不是像216这样的硬编码值。这使它保持了良好的和未来的证明,在(不太可能的)事件中,键盘的大小曾经被苹果改变过。此信息在NSN通知中提供。要获取通知,请在如下位置注册:

- (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.   
    }];
}

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

不,它是一个输入字段,就像在Messages应用程序中一样,需要在底部,并且可以调整大小以适应文本。啊,我明白了。这是输入字段本身。视图是如何向上滑动的?通过调整frame.origin.y值。我通常通过变换来完成这样的调整。我用了一个我已经用过几次的深入但相当可靠的解决方案编辑了答案。它非常有效。显然,改变原点.y导致了这种strage行为。谢谢我没想到它还能用!