隐藏键盘时视图未正确布局

时间:2014-05-29 08:04:16

标签: ios objective-c ios7 uitextview autolayout

我有TextView。当键盘出现时,TextView的一部分隐藏在键盘后面。因此,我通过减少其底部约束(其superview的底部)来降低其高度,以便用户可以滚动并查看整个文本。

但问题是当键盘消失时它没有将TextView的高度调整为原始高度。但是当我点击它时,它会显示与第一张图像相同的全文。

看起来如何:

enter image description here

键盘出现时的外观:

enter image description here

键盘消失时的外观:

enter image description here

现在,如果我点按TextView,它看起来与第一张图片相同。即调整大小以适当大小:

enter image description here

代码: self.messageViewTextView

// Called when keyboard is going to be displayed
- (void)keyboardWillShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    // get animation info from userInfo
    NSTimeInterval animationDuration;
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

    // Save constant to set back later
    self.bottomConstant = self.noticeMessageBottom.constant;

    // Change bottom space constraint's constant
    self.noticeMessageBottom.constant = keyboardFrame.size.height + 8.0f;

    [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        [self.messageView setNeedsLayout];
        [self.messageView layoutIfNeeded];

        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
    } completion:nil];

    [self.messageView flashScrollIndicators];
}

// Called when keyboard is going to be hidden
- (void)keyboardWillHide:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    // get animation info from userInfo
    NSTimeInterval animationDuration;
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

    // Reset contentOffset
    self.messageView.contentOffset = CGPointZero;

    // Change bottom space constraint's constant
    self.noticeMessageBottom.constant = self.bottomConstant;

    [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        [self.messageView setNeedsLayout];
        [self.messageView layoutIfNeeded];

        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
    } completion:nil];
}

更新:我将背景颜色设置为TextView,并看到高度已正确更改,但部分文字不可见。当我点击它时它变得可见。

enter image description here

注意: 仅当TextView的{​​{1}}为contentOffset时才会发生这种情况。即用户尚未滚动它。如果用户滚动它并且(0,0)不是contentOffset,那么它可以正常工作。

1 个答案:

答案 0 :(得分:0)

我只是在故事板中取消选中TextView的滚动启用属性,而不再提供该问题。

enter image description here