问题
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.
回答1:
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()
回答2:
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
!
回答3:
For Swift 4 that works for me:
textField.tintColor = UIColor.clear
回答4:
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()
来源:https://stackoverflow.com/questions/39503873/disable-blinking-cursor-from-uitextfield-in-swift