如何在iOS 7下创build一个UISwitch,而不是采用背后的视图的背景颜色?
看起来像这样每当closures:
虽然我更喜欢灰色背景。 我真的必须使用UIImageView吗?
以下是我如何更改iOS7 UISwitch的填充颜色。
首先你需要导入QuartzCore。
#import <QuartzCore/QuartzCore.h>
然后设置背景颜色并围绕UISwitch的angular落。
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)]; mySwitch.backgroundColor = [UIColor redColor]; mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this. [self addSubview:mySwitch];
这将给你一个自定义closures(背景)颜色的UISwitch。
希望这可以帮助别人:)
您可以将UISwitch
的setOnTintColor
属性设置为setOnTintColor
的颜色。
您也可以在Interface Builder中将其设置为开关。 只需将UISwitch
的背景颜色设置为所需的颜色(在下面的示例中为白色),然后设置layer.cornerRadius = 16
的用户定义的运行属性:
没有API支持更改UISwitch
的closures填充颜色。
调整tintColor
只会影响轮廓,调整backgroundColor
会影响整个框架,包括圆angular以外的部分。
你要么放置一个适当形状的不透明的UIView
或更容易 – 使用一个自定义的开源实现,如MBSwitch ,它允许你设置closures填充颜色。
您还可以使用图像作为背景,使用[UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"toggle-bg-on"]]; mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"toggle-bg-off"]];
添加到巴里威科夫解决scheme:也设置色调颜色
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)]; mySwitch.backgroundColor = [UIColor redColor]; mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this. mySwitch.tintColor = [UIColor redColor]; [self addSubview:mySwitch];