简体   繁体   English

如何在文本字段和键盘中隐藏蓝色光标?

[英]How to hide a blue cursor in a textfield and a keyboard?

I need to get an empty textfield when my ViewController Appears. 当我的ViewController出现时,我需要得到一个空的文本字段。 I tried this: 我试过这个:

override func viewWillAppear(animated: Bool) {
    linkField.placeholder = "test"
    linkField.text = ""
}

But when it appears the keyboard pops up and a blue cursor blinks in a textField. 但是当它出现时,键盘弹出,蓝色光标在textField中闪烁。 How can I get rid of those ? 我怎么能摆脱那些?

EDIT: 编辑:

I think I didn't explain my question correctly. 我想我没有正确解释我的问题。 I don't need to get an empty cursor, I just need to hide it and the keyboard until user touches the textfield to input something. 我不需要得到一个空光标,我只需要隐藏它和键盘,直到用户触摸文本字段输入内容。

The textfield cursor color is based on the default tint color. 文本字段光标颜色基于默认的色调颜色。 In your case it's blue. 在你的情况下,它是蓝色的。 I would change the tint color to clear color. 我会改变色调颜色以清除颜色。

linkField.tintColor = UIColor.clearColor()

For the edited question, if you just want to dismiss the keyboard then 对于编辑过的问题,如果您只想解雇键盘

linkField.resignFirstResponder()

will dismiss the keyboard and when you want focus again use 会解雇键盘,当你想再次使用焦点时

linkField.becomeFirstResponder()

Create subclass of UITextField and override caretRectForPosition 创建UITextField子类并覆盖caretRectForPosition

import UIKit

class RemoveBlinkCursor: UITextField {
    override init(frame:CGRect)
    {
        super.init(frame:frame)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func caretRectForPosition(position: UITextPosition!) -> CGRect {
        return CGRectZero
    }
}

在此输入图像描述

After your edit answer changed : 编辑答案更改后:

For show and hide keyboard and focus you can use resign and become first responders as said by Vig. 对于显示和隐藏键盘和焦点,您可以使用辞职并成为Vig所说的第一响应者。

Hope this will help to others for removing blinking bar. 希望这将有助于其他人去除闪烁的酒吧。

try this 尝试这个

self.textField.tintColor = [UIColor clearColor];

Hope it helps. 希望能帮助到你。

Swift 3 Xcode 8 Swift 3 Xcode 8

Calling view.endEditing(true) has always worked for me 调用view.endEditing(true)一直对我view.endEditing(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM