Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 13 - UITextField with Placeholder getting app crash

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"

like image 430
PrasathBabu Avatar asked Jun 10 '19 13:06

PrasathBabu


4 Answers

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:

  • Objective-C Version
 #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 
  • Swift Version:
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     } } 

2019/09/25 Update

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)  
like image 70
iDevOrz Avatar answered Sep 16 '22 16:09

iDevOrz


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.

enter image description here

like image 28
Chathuranga Silva Avatar answered Sep 17 '22 16:09

Chathuranga Silva


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 enter image description here

after removing the "_" runtime Var start working

like image 27
AFTAB MUHAMMED KHAN Avatar answered Sep 17 '22 16:09

AFTAB MUHAMMED KHAN


Remove Underscore "_" and try this.

[textfield setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];

It should work for all versions!

like image 28
mohsin Avatar answered Sep 19 '22 16:09

mohsin