ios - 如何检查外观属性是否已设置?

标签 ios objective-c cocoa-touch uikit

有没有办法检查 UIAppearance 代理上是否设置了属性?

假设我想做这样的事情:

self.lineWidth = appearance.lineWidth ? : kDefaultLineWidth;

如果 lineWidth 在代理上设置为零,这将无法正常工作,因为它与根本没有设置(假设它是 CGFloat)无法区分。

最佳答案

您不需要进行该检查;外观系统会为您处理。在您的初始化程序中,将lineWidth 设置为您的默认值使用直接ivar 访问 而不是它的setter:

_lineWidth = kDefaultLineWidth;

如果为 lineWidth 设置了外观属性,这将被覆盖。如果没有,它将不会被覆盖。

当您改为设置 lineWidth 通过它的 setter 时,问题就出现了。外观系统随后会将该实例标记为已定制,并且不会覆盖您的更改。

来源: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.

关于ios - 如何检查外观属性是否已设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30555337/

相关文章:

ios - 设置选项 View 属性的属性时出现 EXC_BAD_ACCESS

ios - 从文档目录获取图像到 Collection View

iphone - 使用 Xcode 创建无线 list 文件(.plist 扩展名)

ios - 具有相同苹果企业帐户的多个企业应用程序

ios - 是否可以将Xamarin ios App连接到Swift WatchOS扩展

iphone - 如何裁剪带 mask 的图像并将其与 iPhone 上的另一个图像(背景)组合? (首选 OpenGL ES 1.1)

iphone - 什么时候调用 viewWillAppear?

iphone - 当应用程序进入后台时 MPMoviePlayerController 被关闭

ios - 快速点击uitableview中的headerview

iphone - 如何在点击后突出显示电子邮件地址