问题描述
上面的黄色方框是一个可编辑的UITextView,当前正在编辑(是第一响应者)。但你可能说得对不对?因为没有光标。但是有......这是黄色textView左下角的小蓝点。它略低于textViews底部边框。因此,如果我继续使用新行,或按Enter键,文本将自然地向上移动。但光标永远不会水平,或者在UITextView的下边框正上方。它总是只是勉强从底部伸出,在边界下面几个点。
That yellow box above is an editable UITextView that is currently being edited (is first responder). But you probably couldn't tell right? Because there is no cursor. But there is... It's that small blue dot in the bottom left of the yellow textView. It's slightly below the textViews bottom border. So if I keep going to a new line, or pressing enter, the text will move up as it naturally should. But the cursor is never "level", or right above the UITextView's bottom border. It's always just barely poking out of the bottom, a couple points below the border.
为什么?这在iOS 6中不是问题。有什么方法可以解决这个问题吗?
Why? This wasn't a problem in iOS 6. Any way to fix this?
推荐答案
这个bug在iOS 7.0中你可以解决这可以通过修改textView委托方法。
this bug is in iOS 7.0 you can solve this by modifying textView delegate method.
尝试下面的代码
- (void)textViewDidChange:(UITextView *)textView {
CGRect line = [textView caretRectForPosition:
textView.selectedTextRange.start];
CGFloat overflow = line.origin.y + line.size.height
- ( textView.contentOffset.y + textView.bounds.size.height
- textView.contentInset.bottom - textView.contentInset.top );
if ( overflow > 0 ) {
// We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
// Scroll caret to visible area
CGPoint offset = textView.contentOffset;
offset.y += overflow + 7; // leave 7 pixels margin
// Cannot animate with setContentOffset:animated: or caret will not appear
[UIView animateWithDuration:.2 animations:^{
[textView setContentOffset:offset];
}];
}
}
你的问题将会解决。
这篇关于在iOS 7中编辑时,UITextView光标无法正确定位。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!