Xcode 11 Beta-iOS 13模拟器-带有占位符的UITextField导致应用崩溃

Pra*_*abu 16 placeholder uitextfield ios13 xcode11

在Xcode 11 Beta版本和iOS 13模拟器中,访问TextField _placeholderLabel.textColor标签键时会崩溃。

用于应用占位符文本颜色的键。

[textfield setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

“ NSGenericException”-原因:“禁止访问UITextField的_placeholderLabel ivar。这是一个应用程序错误”

iDe*_*Orz 18

您可以使用运行时来做到这一点:

将以下代码添加到占位符设置的底部

Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
UILabel *placeholderLabel = object_getIvar(textField, ivar);
placeholderLabel.textColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

在Xcode 11 beta2上,此代码有效,但是我不知道GM版本或正式版本。

完整的代码:

  • Objective-C版本
Ivar ivar =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
UILabel *placeholderLabel = object_getIvar(textField, ivar);
placeholderLabel.textColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)
  • 迅捷版:

#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
Run Code Online (Sandbox Code Playgroud)

2019/09/25更新

以上实现可以解决问题,但不建议这样做。

使用私有API的应用程序将来可能会损坏。

请使用新的api:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

讨论区

默认情况下,此属性为nil。如果设置,则占位符字符串将使用系统定义的颜色以及属性字符串的其余样式信息(文本颜色除外)绘制。为该属性分配新值也将使用相同的字符串数据替换占位符属性的值,尽管没有任何格式信息。为该属性分配新值不会影响文本字段的任何其他与样式相关的属性。

完整的代码:

var attributedPlaceholder: NSAttributedString? { get set }
Run Code Online (Sandbox Code Playgroud)


Cha*_*lva 14

我认为这是 XCode 方面的一个错误,希望他们能在下一个版本中修复这个错误。您可以通过擦除模拟器设备上的所有内容和设置来快速解决此问题。

在此处输入图片说明


aft*_*han 12

是的,这个问题与 Xcode 版本有关。我有 Xcode 11.2.1,我面临同样的问题。

_placeholderLabel.textColor
Run Code Online (Sandbox Code Playgroud)

如果您将故事板中的它用作运行时变量并遇到问题,则只需删除“_”

像那样 在此处输入图片说明

删除“_”运行时 Var 后开始工作


moh*_*sin 10

删除下划线“_”并尝试此操作。

[textfield setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

它应该适用于所有版本!


Mic*_*tte 5

我是这样做的:

对象

NSMutableAttributedString *placeholderAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:searchField.attributedPlaceholder];
[placeholderAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [placeholderAttributedString length])];
searchField.attributedPlaceholder = placeholderAttributedString;
Run Code Online (Sandbox Code Playgroud)

迅捷5

var placeholderAttributedString = NSMutableAttributedString(attributedString: searchField.attributedPlaceholder)
    placeholderAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: placeholderAttributedString.length))
    searchField.attributedPlaceholder = placeholderAttributedString
Run Code Online (Sandbox Code Playgroud)

有点长,但是我暂时没有更好的选择。