How can I check if an appearance property is set?

Is there a way to check if a property is set in the UIAppearance proxy or not?

Let's say I want to do something like:

self.lineWidth = appearance.lineWidth ? : kDefaultLineWidth;

      

This will not work correctly if the proxy was set to zeroWidth as it would be indistinguishable from not having it set at all (assuming it is CGFloat).

+3


source to share


1 answer


You don't need to do this check; the appearance system will take care of this for you. In your initializer, set the lineWidth

default using direct access to the ivar instead of its setter:

_lineWidth = kDefaultLineWidth;

      

If the lineWidth

appearance property is set to, this will be overridden. If not, it will not be canceled.



The problem arises if you instead lineWidth

install it via your setter. The spawn system will mark this instance as configured and will not undo your changes.

Source: http://petersteinberger.com/blog/2013/uiappearance-for-custom-views/

One of them is that UIAppearance checks all default setters and keeps track of when they change, so UIAppearance does not override your settings. [If] we use setters in the initializer ... for UIAppearance it now looks like we have already configured this class ourselves. Lesson: use only direct ivar access in initializer for properties matching UI_APPEARANCE_SELECTOR.

+1


source







All Articles