iOS 8中UITextView出现不需要的内容缩进是由什么原因引起的?(在iOS 7中没有这个问题)

5

我一直在使用自动布局通过编程方式创建iOS 7中的UITextView,就像这样:

_textView = [UITextView new];
_textView.translatesAutoresizingMaskIntoConstraints = NO;
_textView.font = [UIFont systemFontOfSize:18.0];
_textView.delegate = self;
[self.view addSubview:_textView];

除了我已经创建的其他限制,我还为文本视图底部创建了这个限制:

_textViewBottomConstraint = 
       [NSLayoutConstraint constraintWithItem:_textView
                                                     attribute:NSLayoutAttributeBottom
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.bottomLayoutGuide
                                                     attribute:NSLayoutAttributeTop
                                                    multiplier:1.0
                                                      constant:0.0];

[self.view addConstraint:_textViewBottomConstraint];

当键盘显示时,我希望能够更改约束条件:

- (void)keyboardDidShow:(NSNotification*)sender
{
    CGRect keyboardFrame = 
                        [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect convertedFrame = 
                        [self.view convertRect:keyboardFrame fromView:self.view.window];
    _textViewBottomConstraint.constant = -convertedFrame.size.height;
    [_textView layoutIfNeeded];
}

一切都运行得很好...

问题

我刚刚转换到Xcode 6和iOS 8(晚了,我知道) - 现在UITextView中的文本顶部有一个插入。例如(文本视图背景为黄色):

enter image description here

是什么导致了这个插入?它与NSTextContainertextContainerInset有关吗?

  • 使用NSTextContainertextContainerInset是从iOS 7开始正确使用UITextView的方法吗?
  • 在iOS 8中必须这样做吗?

如果是,我希望能够提供一些指导,以编程方式创建UITextView以及必要的NSTextContainerNSLayoutManager;然后设置textContainerInset。(我将使用自动布局,以编程方式实现)。

谢谢。

2个回答

9

我认为automaticallyAdjustsScrollViewInsets在iOS 8中的工作方式有所不同。在7中,它仅适用于控制器的view属性是scrollView的情况下。而在8中,它似乎适用于控制器视图的子视图中的滚动视图。


7

这是来自UIViewControllercontentInset

似乎在iOS 8中,我的UITextViewUIViewController中,需要显式设置:

self.automaticallyAdjustsScrollViewInsets = NO;

没有明确设置这个,我检查了一个运行iOS 7的物理iPhone 5s和运行iOS 8的模拟器iPhone 5s之间的差异。

下面是一些日志输出,检查textContainerInsetcontentInsetscrollIndicatorInset的顶部值。在每种情况下,在viewDidLoadkeyboardDidShow之后。

iOS 7上运行的iPhone 5s设备

2014-09-12 07:55:01.244 Alter[19147:60b] viewDidLoad
2014-09-12 07:55:01.249 Alter[19147:60b] textContainerInset Top: 8.000000
2014-09-12 07:55:01.252 Alter[19147:60b] contentInset Top: 0.000000
2014-09-12 07:55:01.253 Alter[19147:60b] scrollIndicatorInset Top: 0.000000

2014-09-12 07:55:02.515 Alter[19147:60b] keyboardDidShow
2014-09-12 07:55:02.516 Alter[19147:60b] textContainerInset Top: 8.000000
2014-09-12 07:55:02.516 Alter[19147:60b] contentInset Top: 0.000000
2014-09-12 07:55:02.516 Alter[19147:60b] scrollIndicatorInset Top: 0.000000

模拟器 iPhone 5s(或6、6 Plus)运行 iOS 8:

2014-09-12 07:55:39.395 Alter[2120:109535] viewDidLoad
2014-09-12 07:55:39.395 Alter[2120:109535] textContainerInset Top: 8.000000
2014-09-12 07:55:39.396 Alter[2120:109535] contentInset Top: 0.000000
2014-09-12 07:55:39.396 Alter[2120:109535] scrollIndicatorInset Top: 0.000000

2014-09-12 07:55:39.939 Alter[2120:109535] keyboardDidShow
2014-09-12 07:55:39.939 Alter[2120:109535] textContainerInset Top: 8.000000
2014-09-12 07:55:39.939 Alter[2120:109535] contentInset Top: 64.000000
2014-09-12 07:55:39.939 Alter[2120:109535] scrollIndicatorInset Top: 64.000000

为了回答我自己的问题,我不需要手动设置NSTextContainerNSLayoutManager。虽然使用textContainerInset方法可能更好,但如果我在父视图控制器中设置这个,我可以调整框架大小。
self.automaticallyAdjustsScrollViewInsets = NO;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接