Change color of UISwitch in “off” state

前端 未结 18 2713
忘掉有多难
忘掉有多难 2020-11-30 20:53

I\'ve learned that we can change the UISwitch button appearance in its \"on\" state, but is it also possible to change the color of the UISwitch in the \"off\" state?

18条回答
  •  [愿得一人]
    2020-11-30 21:42

    objective c category to use on any UISwitch in project using code or storyboard:

    #import 
    
    @interface UISwitch (SAHelper)
    @property (nonatomic) IBInspectable UIColor *offTint;
    @end
    

    implementation

    #import "UISwitch+SAHelper.h"
    
    @implementation UISwitch (SAHelper)
    @dynamic offTint;
    - (void)setOffTint:(UIColor *)offTint {
        self.tintColor = offTint;   //comment this line to hide border in off state
        self.layer.cornerRadius = 16;
        self.backgroundColor = offTint;
    }
    @end
    

提交回复
热议问题