Reputation: 5890
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.
Upvotes: 25
Views: 29345
Reputation: 1956
Solution for SwiftUI
@State var text: String = ""
var body: some View {
TextField("Placeholder Text", text: $text)
.accentColor(.clear)
}
Upvotes: 2
Reputation: 19
you can change tintcolor
self.youtextField().tintColor = UIColor.clearColor()
or
you make a class for text field
Upvotes: 1
Reputation: 27428
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
!
Upvotes: 55
Reputation: 416
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()
Upvotes: 2
Reputation: 82759
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()
Upvotes: 7