Ios 按钮开关更改默认颜色

Ios 按钮开关更改默认颜色,ios,uiswitch,Ios,Uiswitch,如何更改UISwitch按钮的颜色?当它出现在模拟器中时,它不应该处于打开,而应该处于关闭。我想看到它是白色的,激活时,它应该变成绿色。创建一个用于更改开关值的处理程序(见下文)。也不要忘记设置初始颜色,这取决于开关的状态 - (void)onFlipSwitch:(UISwitch *)aSwitch { if(aSwitch.on) { aSwitch.backgroundColor = [UIColor greenColor]; } else {

如何更改UISwitch按钮的颜色?当它出现在模拟器中时,它不应该处于
打开
,而应该处于
关闭
。我想看到它是白色的,激活时,它应该变成绿色。

创建一个用于更改开关值的处理程序(见下文)。也不要忘记设置初始颜色,这取决于开关的状态

- (void)onFlipSwitch:(UISwitch *)aSwitch {
    if(aSwitch.on) {
        aSwitch.backgroundColor = [UIColor greenColor];
    }
    else {
        aSwitch.backgroundColor = [UIColor whiteColor];
    }
}

您可以访问UISwitchView的onTintColor和tintColor属性

@property(nonatomic, retain) UIColor *onTintColor
@property(nonatomic, retain) UIColor *tintColor
初始化开关时,将onTintColor设置为所需颜色的On值,将tintColor设置为Off值。 例如,在UIViewController中:

    UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 41.0, 30.0)];

    // on color
    theSwitch.onTintColor = [UIColor blueColor];

    //off color
    theSwitch.tintColor = [UIColor redColor];

    [self.view addSubview:theSwitch];