如何使 UIAppearance 代理适用于以编程方式创建的视图?
How to make UIAppearance proxies apply to programmatically created views?
我正在尝试使用 UIAppearance 为我的应用创建主题。
假设我希望我所有 UILabel
的文本都是橙色的。我写道:
UILabel.appearance(whenContainedInInstancesOf: [UIView.self]).textColor = .orange
但我意识到:
- 这适用于通过 Interface Builder 创建的每个
UILabel
。
- 这不适用于任何以编程方式创建的
UILabel
。它们默认显示为黑色
知道如何强制以编程方式创建的实例也变成橙色吗?
PS:我也试过:UILabel.appearance().textColor = .green
这不是我想要的,但它也不起作用。
经过一番搜索...
显然,在 Interface Builder 中创建的 UI 个元素(例如标签) 受 .appearance()
设置并不是故意的,可能不可靠。
事实上,文档似乎相当混乱和误导。
To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR.
唯一采用 UIAppearance
协议的 class 是
UIBarItem
UIView
唯一采用 UIAppearanceContainer
协议的 class 是
UIPopoverController
UIPresentationController
UIView
UIViewController
所以...最好的办法可能是 class 您的标签。
可在此处找到相关答案:
来自link:
"UIAppearance only allows you to set properties that are flagged with UI_APPEARANCE_SELECTOR in the Objective-C header file. Since UILabel has no such properties, it cannot be styled with UIAppearance"
也来自:https://developer.apple.com/documentation/uikit/uiappearance
"iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back."
也来自这里:trouble with UIAppearance and UIButton subclassing
"The reason it's working this way is because appearance properties are applied when the view is added to a view hierarchy, and nib-defined properties are applied before the view is added to a view hierarchy."
在以编程方式将该视图控制器添加到层次结构之前,我尝试在视图控制器的新 class 实例中设置标签的外观,但没有成功。
我正在尝试使用 UIAppearance 为我的应用创建主题。
假设我希望我所有 UILabel
的文本都是橙色的。我写道:
UILabel.appearance(whenContainedInInstancesOf: [UIView.self]).textColor = .orange
但我意识到:
- 这适用于通过 Interface Builder 创建的每个
UILabel
。 - 这不适用于任何以编程方式创建的
UILabel
。它们默认显示为黑色
知道如何强制以编程方式创建的实例也变成橙色吗?
PS:我也试过:UILabel.appearance().textColor = .green
这不是我想要的,但它也不起作用。
经过一番搜索...
显然,在 Interface Builder 中创建的 UI 个元素(例如标签) 受 .appearance()
设置并不是故意的,可能不可靠。
事实上,文档似乎相当混乱和误导。
To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR.
唯一采用 UIAppearance
协议的 class 是
UIBarItem
UIView
唯一采用 UIAppearanceContainer
协议的 class 是
UIPopoverController
UIPresentationController
UIView
UIViewController
所以...最好的办法可能是 class 您的标签。
可在此处找到相关答案:
来自link: "UIAppearance only allows you to set properties that are flagged with UI_APPEARANCE_SELECTOR in the Objective-C header file. Since UILabel has no such properties, it cannot be styled with UIAppearance"
也来自:https://developer.apple.com/documentation/uikit/uiappearance
"iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back."
也来自这里:trouble with UIAppearance and UIButton subclassing
"The reason it's working this way is because appearance properties are applied when the view is added to a view hierarchy, and nib-defined properties are applied before the view is added to a view hierarchy."
在以编程方式将该视图控制器添加到层次结构之前,我尝试在视图控制器的新 class 实例中设置标签的外观,但没有成功。