什么是在iOS 8中使用UITextView导致此不需要的内容(在iOS 7中没有)?

Gav*_*ope 5 nslayoutmanager uitextview nstextcontainer ios7 ios8

UITextView在iOS 7中,我一直在使用自动布局以编程方式创建.这样的事情:

_textView = [UITextView new];
_textView.translatesAutoresizingMaskIntoConstraints = NO;
_textView.font = [UIFont systemFontOfSize:18.0];
_textView.delegate = self;
[self.view addSubview:_textView];
Run Code Online (Sandbox Code Playgroud)

除了我创建的其他约束之外,我还有一个用于文本视图的底部:

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

[self.view addConstraint:_textViewBottomConstraint];
Run Code Online (Sandbox Code Playgroud)

这样我可以在键盘显示时更改约束:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好......

问题

我刚刚切换到Xcode 6和iOS 8(我知道很晚) - UITextView现在的文本顶部有一个插页.例如(文本视图背景为黄色):

在此输入图像描述

是什么导致了插图?是否NSTextContainertextContainerInset?相关?

  • 正在与NSTextContainertextContainerInset正确使用方式UITextView从iOS的7及以后?
  • 是否必须在iOS 8中以这种方式完成?

如果是的话,我会感谢一些指导,以编程方式创建一个UITextView必要的NSTextContainerNSLayoutManager; 然后设置textContainerInset.(我将以编程方式使用自动布局).

谢谢.

jos*_*hrl 9

我认为在iOS 8中,automaticAdjustsScrollViewInsets的工作方式略有不同.在7中,它仅在控制器的view属性为scrollView时应用.在8中,它似乎适用于作为控制器视图的子视图的滚动视图.


Gav*_*ope 7

这是contentInset来自UIViewController.

似乎在iOS 8中,我和我的内容UITextView一样UIViewController,需要明确设置:

self.automaticallyAdjustsScrollViewInsets = NO;
Run Code Online (Sandbox Code Playgroud)

没有明确设置,我已经检查了运行iOS 7的物理iPhone 5和运行iOS 8的模拟器iPhone 5s之间的区别.

这是一些日志输出,检查最高值textContainerInset,contentInsetscrollIndicatorInset.在每种情况下,之后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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

要回答我自己的问题,我不需要手动设置NSTextContainerNSLayoutManager.虽然开始使用该textContainerInset方法可能更好,但如果我在父视图控制器中设置它,我可以选择调整框架:

self.automaticallyAdjustsScrollViewInsets = NO;
Run Code Online (Sandbox Code Playgroud)