In iOS 13, I'm getting a crash when accessing the UITextField
_placeholderLabel.textColor label key.
The key used to apply placeholder text color.
[textfield setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
"NSGenericException" - reason: "Access to UITextField's _placeholderLabel ivar is prohibited. This is an application bug"
You can do it by using runtime:
add the following code to the bottom of placeholder setting
Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel"); UILabel *placeholderLabel = object_getIvar(textField, ivar); placeholderLabel.textColor = [UIColor whiteColor];
At Xcode 11 beta2 ,this code is work ,but I don't know about GM version or official version.
The complete code:
#import "ViewController.h" #import <objc/runtime.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; self.title = @"UITextField Demo"; UITextField *textField = [UITextField new]; textField.frame = CGRectMake(0, 100, 300, 50); textField.placeholder = @"UITextField Demo"; [self.view addSubview:textField]; Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel"); UILabel *placeholderLabel = object_getIvar(textField, ivar); placeholderLabel.textColor = [UIColor whiteColor]; } @end
import UIKit class ViewController: UIViewController { override func viewDidLoad() { let textField = UITextField() textField.frame = CGRect(x: 0, y: 100, width: 300, height: 50) textField.placeholder = "UITextField Demo" view.addSubview(textField) let iVar = class_getInstanceVariable(UITextField.self, "_placeholderLabel")! let placeholderLabel = object_getIvar(textField, iVar) as! UILabel placeholderLabel.textColor = .red } }
The above implementation can solve the problem ,but it not be advocated.
The apps that use the private api maybe broken in the future.
Please use new api :
var attributedPlaceholder: NSAttributedString? { get set }
Discussion
This property is nil by default. If set, the placeholder string is drawn using system-defined color and the remaining style information (except the text color) of the attributed string. Assigning a new value to this property also replaces the value of the placeholder property with the same string data, albeit without any formatting information. Assigning a new value to this property does not affect any other style-related properties of the text field.
The complete code:
let textField = UITextField() textField.frame = CGRect(x: 0, y: 100, width: 300, height: 50) let placeholderString = NSAttributedString.init(string: "UITextField Demo", attributes: [NSAttributedString.Key.foregroundColor : UIColor.red]) textField.attributedPlaceholder = placeholderString view.addSubview(textField)
I think it's a bug from the XCode side and hopefully, they will fix this one on the next release. You can quickly fix this by Erase all Contents and Settings on the Simulator device.
Yes this problem is related with the Xcode version. I have Xcode 11.2.1 and i am facing same issue.
_placeholderLabel.textColor
if you are using it from storyboard as runtime var and getting issue so just remove "_"
like that
after removing the "_" runtime Var start working
Remove Underscore "_" and try this.
[textfield setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
It should work for all versions!
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