我一直在使用自动布局通过编程方式创建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
中的文本顶部有一个插入。例如(文本视图背景为黄色):
是什么导致了这个插入?它与NSTextContainer
和textContainerInset
有关吗?
- 使用
NSTextContainer
和textContainerInset
是从iOS 7开始正确使用UITextView
的方法吗? - 在iOS 8中必须这样做吗?
如果是,我希望能够提供一些指导,以编程方式创建UITextView
以及必要的NSTextContainer
和NSLayoutManager
;然后设置textContainerInset
。(我将使用自动布局,以编程方式实现)。
谢谢。