在iOS 7中编辑时,UITextView光标无法正确定位。为什么?

时间:2013-10-08 22:47:43

标签: ios uiscrollview ios7 uitextview

Why is this a thing thats happening

上面的黄色方框是一个可编辑的UITextView,当前正在编辑(是第一响应者)。但你可能说得对不对?因为没有光标。但是有......这是黄色textView左下角的小蓝点。它略低于textViews底部边框。因此,如果我继续使用新行,或按Enter键,文本将自然地向上移动。但是光标永远不会“水平”,或者在UITextView的下边框正上方。它总是只是从底部勉强伸出来,在边界下面几个点。

为什么呢?这在iOS 6中不是问题。有什么方法可以解决这个问题吗?

5 个答案:

答案 0 :(得分:41)

这个bug在iOS 7.0中你可以通过修改textView委托方法来解决这个问题。

尝试以下代码

- (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];
        }];
    }
}

你的问题将会解决。

答案 1 :(得分:3)

如果您处于导航控制器场景中,我认为这实际上是iOS 7中引入的属性的结果:automaticallyAdjustsScrollViewInsets(请参阅iOS 7 transition guide

默认为YES,并通过调整导航控制器高度的滚动偏移量来尝试使用UITextView(它本身就是滚动视图)变得聪明。

因此,解决方案是在viewDidLoad中将此属性设置为NO(小心不要在iOS 6上调用它,因为它仅在iOS 7中可用)。

答案 2 :(得分:-1)

根据Todd给出的答案,您可以在应用程序完成启动时运行的方法中放置类似的内容:

   [[UITextView appearance] setTintColor:[UIColor redColor]];
   [[UITextField appearance] setTintColor:[UIColor redColor]];

这将影响所有UITextFields和UITextViews。

答案 3 :(得分:-1)

- (BOOL) textView:(UITextView *)sender shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSString * newText = [sender.text stringByReplacingCharactersInRange:range withString:text];
    [self setFramesForText:newText];

    if (!IOS_VERSION_6_OR_LESS) {
        if ([text isEqualToString:@"\n"]){
            [textView setContentOffset:CGPointMake(textView.contentOffset.x, 999999999)];
        }
    }

    return YES;
}

答案 4 :(得分:-3)

您只需要设置UITextView的tintColor。

在代码中放入类似下面的代码(我将它放在viewDidLoad中),对我有效。

self.textView.tintColor = [UIColor redColor];
相关问题