If you're targeting iOS 7+, this has been made much easier. Simply change the tintColor
of the field with a cursor using the appearance proxy and it will apply throughout the app:
Swift 3.0:
UITextField.appearance().tintColor = .black
Objective-C:
[[UITextField appearance] setTintColor:[UIColor blackColor]];
Same answer applies for an individual UITextField
:
Swift 3.0:
myTextField.tintColor = .black
Objective-C
[myTextField setTintColor:[UIColor blackColor]];
With iOS7 you can simply change tintColor of the textField
Swift 3:
UITextField.appearance().tintColor = UIColor.black
UITextView.appearance().tintColor = UIColor.black
yourTextField.tintColor = [UIColor whiteColor];
It works if you set it in code, 'cos somehow color trigger doesn't do it in the Interface Builder (Xcode 6.1.1). It suited well without a need to change any appearance proxy.
Note: This answer is out of date and should be used for pre-iOS 7 development only. See other answers for a 1 line solution using the appearance proxy in iOS 7.
I arrived at this question after I faced the same problem in a project I was working on.
I managed to create a solution that will be accepted by the AppStore review team as it does not use any existing Private APIs.
I have created a control called DGTextField that extends UITextField.
Setting tintColor
for UITextField
and UITextView
works differently.
While for UITextField
you don't need to call additional code after updating tintColor
to change cursor color, but for UITextView
you need.
So after setting tintColor
for UITextView
(it doesn't matter in IB or in code) you need to call textView.tintColorDidChange()
in order to apply it (actually it will pass text view's config down to its subviews hierarchy).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With