【问题标题】:Disable blinking cursor from UITextField in swift?快速禁用 UITextField 中的闪烁光标?
【发布时间】:2017-01-23 01:16:00
【问题描述】:

如何在 Swift 中从 UITextField 禁用光标?条件 - 当用户登录时,然后进入应用程序。如果用户可以让我从应用程序中注销,则不在 UITextField 中显示光标。

【问题讨论】:

  • 简单地将当前文本字段的色调更改为tintColor = [UIColor clearColor]
  • 你确认 UITextFieldDelegate 了吗?

标签: ios swift uitextfield swift3


【解决方案1】:

如果您只是不想要cursor,那么您可以将它的tintColor 设置为clearColor。在这种情况下,您的 textField 将处于活动状态。如果你想让它成为非活动字段,那么你应该调用textfieldresignFirstresponder方法!

【讨论】:

  • 这应该是选择的答案。
【解决方案2】:

对于适合我的 Swift 4:

textField.tintColor = UIColor.clear

【讨论】:

    【解决方案3】:

    最初在user default中设置了一些bool值,当用户按下注销按钮时将值设置为NO

    例如

    UserDefaults.standard().set(true, forKey: "disalert")
    

    当你来到这个页面时,检查条件就像

    let disalert: Bool = UserDefaults.standard().bool(forKey: "disalert")
    
          self.youtextField().tintColor = UIColor.blueColor()
        if disalert {
            self.youtextField().tintColor = UIColor.clearColor()
            self.youtextField().resignFirstresponder()
         }
    

    当用户按下登录按钮时,将用户默认值设置为

    UserDefaults.standard().set(false, forKey: "disalert")
    

    像这样使用

    (self.youtextField.valueForKey("textInputTraits") as! String)["insertionPointColor"] = UIColor.clearColor()
    

    或设置类似

     self.youtextField().tintColor = UIColor.clearColor()
    

    【讨论】:

    • self.youtextField().tintColor = UIColor.clearColor() 为我工作
    【解决方案4】:

    首先你需要确认 UITextFieldDelegate 然后设置你的 textField 比如yourTextField.delegate =self 添加 yourTextField.resignFirstResponder()

    到您的代码。调用 resignFirstResponder 到 yourTextField。 您也可以使用以下代码(根据您的需要)。

    在委托方法 textFieldDidEndEditing(textField: UITextField) 中检查(如果)它是否有一些值/文本。

    yourTextField.attributedPlaceholder = NSAttributedString(string: "User Name", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()]) textUserName.resignFirstResponder()

    【讨论】:

      【解决方案5】:

      你可以改变色调

      self.youtextField().tintColor = UIColor.clearColor()

      你为文本字段创建一个类

      【讨论】:

        【解决方案6】:

        SwiftUI 解决方案

        @State var text: String = ""
        
        var body: some View {
            TextField("Placeholder Text", text: $text)
                .accentColor(.clear)
        }
        

        【讨论】:

          猜你喜欢
          • 2011-10-30
          • 2011-07-31
          • 1970-01-01
          • 2012-03-10
          • 1970-01-01
          • 2012-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多