如何检查外观 属性 是否已设置?
How can I check if an appearance property has been set?
有没有办法检查 属性 是否已在 UIAppearance 代理上设置?
假设我想做类似的事情:
self.lineWidth = appearance.lineWidth ? : kDefaultLineWidth;
如果在代理上将 lineWidth 设置为零,这将无法正常工作,因为它与根本没有设置(假设它是 CGFloat)无法区分。
您不需要进行该检查;外观系统会为您处理。在您的初始化程序中,将 lineWidth
设置为您的默认值 使用直接 ivar 访问 而不是它的 setter:
_lineWidth = kDefaultLineWidth;
如果为 lineWidth
设置了外观 属性,这将被覆盖。如果没有,它将不会被覆盖。
当您改为通过 setter 设置 lineWidth
时,就会出现问题。外观系统随后会将该实例标记为已定制,并且不会覆盖您的更改。
来源:http://petersteinberger.com/blog/2013/uiappearance-for-custom-views/
One gotcha is that UIAppearance swizzles all setters that have a default apperance, and tracks when they get changed, so that UIAppearance doesn’t override your customizations. [If] we use setters in the initializer, ...for UIAppearance it now looks as though we already customized the class ourselves. Lesson: Only use direct ivar access in the initializer for properties that comply to UI_APPEARANCE_SELECTOR.
有没有办法检查 属性 是否已在 UIAppearance 代理上设置?
假设我想做类似的事情:
self.lineWidth = appearance.lineWidth ? : kDefaultLineWidth;
如果在代理上将 lineWidth 设置为零,这将无法正常工作,因为它与根本没有设置(假设它是 CGFloat)无法区分。
您不需要进行该检查;外观系统会为您处理。在您的初始化程序中,将 lineWidth
设置为您的默认值 使用直接 ivar 访问 而不是它的 setter:
_lineWidth = kDefaultLineWidth;
如果为 lineWidth
设置了外观 属性,这将被覆盖。如果没有,它将不会被覆盖。
当您改为通过 setter 设置 lineWidth
时,就会出现问题。外观系统随后会将该实例标记为已定制,并且不会覆盖您的更改。
来源:http://petersteinberger.com/blog/2013/uiappearance-for-custom-views/
One gotcha is that UIAppearance swizzles all setters that have a default apperance, and tracks when they get changed, so that UIAppearance doesn’t override your customizations. [If] we use setters in the initializer, ...for UIAppearance it now looks as though we already customized the class ourselves. Lesson: Only use direct ivar access in the initializer for properties that comply to UI_APPEARANCE_SELECTOR.