ios - TextView文本未正确显示

标签 ios objective-c ios7 uitextview autolayout

我使用自动布局。我有一个包含其他 View 的 TextView。当键盘出现时,部分 TextView 隐藏在键盘后面。因此,我通过降低其底部约束(及其父 View 的底部)来降低其高度,以便用户可以滚动并查看整个文本。

但问题是当键盘消失时,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];
}

注意:只有当 TextViewcontentOffset(0 ,0).即用户没有滚动它或滚动回顶部。如果用户滚动它并且 contentOffset 不是 (0,0) 那么它工作正常。

最佳答案

我只是取消选中 Storyboard中 TextView 的滚动启用属性,它不再出现该问题。

enter image description here

关于ios - TextView文本未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929490/

相关文章:

c++ - Cmake 在 Mac OSX 上使用框架编译并将 .cpp 文件视为 .m/.mm

objective-c - 10.10 中通知中心 .db 文件的新位置?

iphone - 使用 NSLayoutManager 计算每个字形的帧

ios7 - 当应用程序进入后台时,startAdvertisingPeer 是否工作?

ios - 绘制世界地图 - 性能和交互 - iOS

ios - 带加密的 HTTP 直播

ios - UIView 可以复制吗?

ios - 在 iOS 中自动播放 vimeo 视频

android - - 如何修复它 : Incorrect use of ParentDataWidget?

ios - 应用程序不响应 iOS 7 中的触摸/点击