Ios7 是什么原因导致在iOS 8中使用UITextView插入这种不需要的内容(iOS 7中没有)?
我一直在iOS 7中使用自动布局以编程方式创建Ios7 是什么原因导致在iOS 8中使用UITextView插入这种不需要的内容(iOS 7中没有)?,ios7,uitextview,ios8,nslayoutmanager,nstextcontainer,Ios7,Uitextview,Ios8,Nslayoutmanager,Nstextcontainer,我一直在iOS 7中使用自动布局以编程方式创建UITextViews。大概是这样的: _textView = [UITextView new]; _textView.translatesAutoresizingMaskIntoConstraints = NO; _textView.font = [UIFont systemFontOfSize:18.0]; _textView.delegate = self; [self.view addSubview:_textView]; 除了我创建的其他
UITextView
s。大概是这样的:
_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中的文本现在在顶部有一个插图。例如(文本视图背景为黄色):
是什么导致了插图?它是否连接到NSTextContainer
和TextContainerSet
- 使用
和NSTextContainer
是否是从iOS 7及以后使用textContainerInset
的正确方法UITextView
- 在iOS 8中是否需要这样做
UITextView
以及必要的NSTextContainer
和NSLayoutManager
;然后设置textContainerInset
。(我将以编程方式使用自动布局)
谢谢。这是来自
UIViewController
的contentInset
似乎在iOS 8中,我的UITextView
在我的UIViewController
中,需要显式设置:
self.automaticallyAdjustsScrollViewInsets = NO;
在没有明确设置的情况下,我检查了运行iOS 7的物理iPhone 5s和运行iOS 8的模拟器iPhone 5s之间的差异
下面是一些日志输出,检查textContainerInset
、contentInset
和scrollindicationset
的最大值。在每种情况下,在viewDidLoad
和keyboardidshow
之后
运行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
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
运行iOS 8的模拟器iPhone 5s(或6或6 Plus):
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
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
要回答我自己的问题,我不需要手动设置NSTextContainer
和NSLayoutManager
。虽然开始使用textContainerInset
方法可能更好,但如果在父视图控制器中设置此选项,我可以选择调整帧:
self.automaticallyAdjustsScrollViewInsets = NO;
我认为自动调整滚动视图插图在iOS 8中的工作方式有点不同。在7中,它仅在控制器的view属性为scrollView时应用。在8中,它似乎适用于作为控制器视图的子视图的滚动视图