tombenner / nui

Style iOS apps with a stylesheet, similar to CSS
MIT License
3.76k stars 461 forks source link

UISwitch support #41

Closed benvium closed 11 years ago

benvium commented 11 years ago

Hiya. I'm trying to add support for setting the onTintColor of a UISwitch to my new fork of this library with the property on-tint-color.

E.g.

Switch {
    on-tint-color: @primaryBackgroundTintColor;
}

I've been copying the way the other elements are styled (e.g. adding UISwitch+NUI and NUISwitchRenderer classes) and using similar implementations.

However, I'm running into an infinite loop issue with the code in UISwitch+NUI.m. This is identical to the implementations for UIView+NUI and UIButton+NUI etc, apart from the UISwitch class test.

- (void)override_didMoveToWindow
{
    if (!self.nuiIsApplied) {
        // Styling shouldn't be applied to inherited classes
        if ([self class] == [UISwitch class]) {
            [self initNUI];
            [self didMoveToWindowNUI];
        }
        self.nuiIsApplied = [NSNumber numberWithBool:YES];
    }
    [self override_didMoveToWindow];
}

What I find particularly odd is how this code doesn't cause infinite loops in your other classes? Surely if self.nuiIsApplied is true, the code will loop forever? I presume I'm missing something about the internals of Obj-C here.

I can commit + push to my branch so you can see the code if this is useful. NOTE: If I remove the [self override_didMoveToWindow] call, then actually it does work and the UISwitch is correctly themed.

Thanks!

tombenner commented 11 years ago

Thanks for adding this! The infinite loop was created because [self swizzleDidMoveToWindow:[UISwitch class]]; wasn't being called in NUISwizzler, so I just added that in and then added support for a few more properties.

It'd be good to have font-* properties on UISwitch's labels at some point, too, but I think that'll involve looping through its subviews, which I don't quite have time for right now, but may add later on (anyone else is welcome to add it in, too).