1。state (状态):默认off
设置状态:[ mySwitch setOn : YES ] ;
获取状态:[ mySwitch isOn ]
2。事件:
[ mySwitch addTarget : self action : @selector ( switchIsChanged: )
forControlEvents : UIControlEventValueChanged ] ;
- (void) switchIsChanged : (UISwitch *) sender {
if ( [ sender isOn ] ) {。。。 }
}
3。颜色:
tintColor:color for off (default state)
onTintColor : color for on
thumbTintColor : switch控件当中球的颜色
p.s. 在ios6和ios7中,上述颜色显示效果略有不同
4。背景图像:(尺寸:77*22 / 154 * 44)
onImage : state for on-mode
offImage : state for off-mode
e.g. mySwitch.onImage = [ UIImage imageNamed : @"On" ] ; // 此处图像名不用加.png
p.s. 上述图像属性只在ios7以前的版本有效
5。代码初始化:(控件通用)
UISwitch *mySwitch = [ [ UISwitch alloc ] initWithFrame : CGRectZero ] ; //用CGRectZero初始化
mySwitch.center = self.view.center ; // 定位控件中心
[ self.view addSubview : mySwitch ] ;
p.s. 也可在IB(界面)上直接完成