Disable blinking cursor from UITextField in swift?

2019-03-29 05:27发布

How to disable cursor in Swift from UITextField? Condition - When the user logs in, then enter into application. If user can may me logout from application then not show cursor into UITextField.

4条回答
放我归山
2楼-- · 2019-03-29 06:01

initially set the some bool value in user default, when user press the logout button set the value as NO

for e.g

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

when ever you comes to this page check the condition like as

let disalert: Bool = UserDefaults.standard().bool(forKey: "disalert")

      self.youtextField().tintColor = UIColor.blueColor()
    if disalert {
        self.youtextField().tintColor = UIColor.clearColor()
        self.youtextField().resignFirstresponder()
     }

when ever user press the login button set the user default value as

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

use like

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

or set like

 self.youtextField().tintColor = UIColor.clearColor()
查看更多
干净又极端
3楼-- · 2019-03-29 06:22

if you just don't want cursor then you can set it's tintColor to clearColor. In this case your textField will be active. If you want to make it inactive field then you should call resignFirstresponder method of textfield!

查看更多
看我几分像从前
4楼-- · 2019-03-29 06:25

First you need to confirm to UITextFieldDelegate and then set you textField like yourTextField.delegate =self add yourTextField.resignFirstResponder()

to your code. Call resignFirstResponder to yourTextField. and you can also use below code (According to your need).

In delegate method textFieldDidEndEditing(textField: UITextField) with checking (if) whether it has some value/text of not.

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

查看更多
混吃等死
5楼-- · 2019-03-29 06:26

For Swift 4 that works for me:

textField.tintColor = UIColor.clear
查看更多
登录 后发表回答