ios 光标未显示在 UITextView 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20959331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:00:20  来源:igfitidea点击:

Cursor not showing up in UITextView

iosiphoneipaduitextfielduitextview

提问by SeanT

Can anyone think of a reason that the blinking cursor would not show up in a UITextView? I have a custom control that is just a subclass of UIViewand it has a UITextViewin it, but the cursor doesn't appear when it gets focus. The keyboard appears, and the text appears as you type, but there is no cursor, and I can't figure out why.

谁能想到闪烁的光标不会出现在 a 中的原因UITextView?我有一个自定义控件,它只是 的一个子类,UIView其中有一个UITextView,但是光标在获得焦点时不会出现。键盘出现,文本在您键入时出现,但没有光标,我不知道为什么。

Any thoughts?...

有什么想法吗?...

回答by lehn0058

You may have changed the tint color in your custom UITextView. If the tint color is the same as the background color (normally white), then it will appear invisible.

您可能已经更改了自定义 UITextView 中的色调颜色。如果色调颜色与背景颜色相同(通常为白色),则它看起来不可见。

回答by Julian Król

You might be setting improperly a contentSizeand/or frame for the component so it is too small to be visible or the control is out of the screen. Please go in Simulator to Debug->Color Blended Layersto see whether those values are set correctly.

您可能未正确设置contentSize组件的a和/或框架,因此它太小而无法看到或控件在屏幕之外。请进入模拟器Debug->Color Blended Layers查看这些值是否设置正确。

EDIT:

编辑:

With new Xcodes (probably introduced in Xcode 6) you can debug this kind of issues by clicking "Debug View Hierarchy" (it is one of the icons on the bottom bar)

使用新的 Xcode(可能在 Xcode 6 中引入),您可以通过单击“调试视图层次结构”(它是底部栏上的图标之一)来调试此类问题

回答by Kunal Gupta

The textfield is showing the cursor but you are not able to see the color, just because the tint color of your text field is set to default most probably as in my case it was. Just select you textField in storyboard and Select the link color of your wish. Refer to the image attached.

文本字段显示光标,但您看不到颜色,只是因为文本字段的色调颜色很可能设置为默认值,就像我的情况一样。只需在情节提要中选择您的 textField 并选择您希望的链接颜色。请参阅所附图片。

enter image description here

在此处输入图片说明

回答by Johnny Rockex

Simple workaround seems to work by introducing a delay and then calling firstResponder, like so:

简单的解决方法似乎可以通过引入延迟然后调用 firstResponder 来工作,如下所示:

-(void)begin{

    [self performSelector:@selector(first) withObject:nil afterDelay:0.01f];
}
-(void)first{

    [tf becomeFirstResponder];
}

回答by Andy Bernard

I changed the tint color of all of my UITextViews and the cursor started showing on the next build.

我更改了所有 UITextViews 的色调颜色,光标开始显示在下一个构建中。

回答by Vladimir Vlasov

In my case, I called becomeFirstResponderin a viewDidLoadmethod. I moved the call into viewDidAppearand it worked for me.

就我而言,我调用becomeFirstResponder了一个viewDidLoad方法。我把电话转了进去viewDidAppear,它对我有用。

回答by TolkienWASP

The solution above did not work in my case. The issue in my case is that the UIViewthat acts as the cursor has isOpaqueset to falsewhen the UITableViewCell's selectionStyleis anything but none. This seems to only occur under a race condition I haven't gotten to the bottom of yet.

上面的解决方案在我的情况下不起作用。在我的情况的问题是,UIView充当光标isOpaque设置为falseUITableViewCellselectionStyle是什么,但none。这似乎只发生在我还没有深入了解的竞争条件下。

FWIW here's how I debugged this:

FWIW 这是我调试的方式:

  1. select your UITextFieldView
  2. open the Debug View Hierarchy. You should see the UIViewthat would represent the cursor in the correct position, but it does not appear in the UI.
  3. right click the cursor's UIViewand select "Print Description...". The description indicates OPAQUE=NO
  1. 选择你的 UITextFieldView
  2. 打开调试视图层次结构。您应该UIView会在正确的位置看到代表光标的 ,但它没有出现在 UI 中。
  3. 右键单击光标UIView并选择“打印说明...”。说明表明OPAQUE=NO

Unfortunately, the UITextSelectionViewclass that owns this UIViewis private and therefore there is no non-hacky way to programmatically update isOpaque.

不幸的是,UITextSelectionView拥有它的类UIView是私有的,因此没有非黑客的方式来以编程方式更新isOpaque.

This doesn't seem to be an issue in every implementation, however, here's the relationship between my views.

这似乎不是每个实现中的问题,但是,这是我的观点之间的关系。

My text view delegate

我的文本视图委托

extension FooViewController: UITextViewDelegate {
    private func updateSelection(selectedIndex indexPath: IndexPath) {
        if let oldSelected = self.table.indexPathForSelectedRow {
            tableDelegate.tableView(table, didDeselectRowAt: oldSelected)
        }
        tableDelegate.tableView(table, didSelectRowAt: indexPath)
    }

    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        guard let rowCell = getParentCell(forTextView: textView), // loops through table row cells
            !rowCell.isSelected,
            let indexPath = self.table.indexPath(for: rowCell)
            else { return true }

        updateSelection(selectedIndex: indexPath)
        return false
    }

and my table delegate:

和我的表代表:

class TableViewDelegate: NSObject, UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {       
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableView.ScrollPosition.none)
    }
}

回答by FD_

For me, the problem was caused by embedding a UITextFieldin a UIToolbar. Once I had replaced the container for a simple UIView, the cursor worked again.

对我来说,问题是由UITextField在 a 中嵌入 a 引起的UIToolbar。一旦我将容器替换为 simple UIView,光标再次工作。