你用UISwitch类来创建和管理开/关机按钮。可以用在设置应用程序的选项,如飞行模式,蓝牙等。UISwitch继承自UIControl类。
1.初始化一个UISwitch对象:
- (instancetype)initWithFrame:(CGRect)frame
例如:
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(40.0f, 160.0f, 50.0f, 28.0f)];
2.设置UISwitch的开关状态(ON/OFF),可选择是否有动画过渡:
- (void)setOn:(BOOL)on
animated:(BOOL)animated
例如:
[switchView setOn:NO animated:YES];
3.设置/确定开关状态(没有动画):
@property(nonatomic, getter=isOn) BOOL on
允许您检索或设置开关状态,例如:
switchView.on = YES;
定制开关外观:
4.处于ON时UISwitch的颜色:
@property(nonatomic, retain) UIColor *onTintColor
5.处于OFF时Switch的颜色:
@property(nonatomic, retain) UIColor *tintColor
6.设置拇指(上面可以移动的按钮)的颜色:
@property(nonatomic, retain) UIColor *thumbTintColor
7.设置ON时(不支持iOS7)的图标:
@property(nonatomic, retain) UIImage *onImage
8.设置OFF时(不支持iOS7)的图标:
@property(nonatomic, retain) UIImage *offImage
9.绑定事件响应:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;