Xcode 11 Beta-iOS 13模拟器-带有占位符的UITextField导致应用程序崩溃 [英] Xcode 11 Beta - iOS 13 Simulator - UITextField with Placeholder getting app crash

查看:423
本文介绍了Xcode 11 Beta-iOS 13模拟器-带有占位符的UITextField导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Xcode 11 Beta Version and iOS 13 Simulator getting a crash when access TextField _placeholderLabel.textColor label key.

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

The key used to apply placeholder text color.

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

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

"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];

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

At Xcode 11 beta2 ,this code is work ,but I don't know about GM version or official version.

完整代码:

  • Objective-C版本

#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
        }
    }
    

    2019/09/25更新

    以上实现可以解决问题,但不提倡.

    2019/09/25 Update

    The above implementation can solve the problem ,but it not be advocated.

    使用私有api的应用程序将来可能会损坏.

    The apps that use the private api maybe broken in the future.

    请使用新的API:

    var attributedPlaceholder: NSAttributedString? { get set }
    

    讨论

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

    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.

    完整代码:

    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)
    
    

    这篇关于Xcode 11 Beta-iOS 13模拟器-带有占位符的UITextField导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆