作用:提供二進(jìn)制選項(xiàng)的控件
iOS系統(tǒng)內(nèi)置了UISwithch控件的size,所以通過代碼調(diào)整UISwithch的大小無效.(可以結(jié)合動畫改變大小)
1.簡單的屬性和方法
- 開關(guān)屬于關(guān)閉狀態(tài)時邊框的顏色(注意這是邊框的顏色)
@property(nonatomic, strong) UIColor *tintColor;
- 開關(guān)屬于開啟狀態(tài)時的顏色
@property(nonatomic, strong) UIColor *onTintColor;
- 開關(guān)的狀態(tài)鈕顏色
@property(nonatomic, strong) UIColor *thumbTintColor;
- 整個開關(guān)背景顏色隐孽,設(shè)置之后可以明顯看到一個矩形背景
@property(nonatomic,copy) UIColor *backgroundColor;
- 開關(guān)的狀態(tài)
@property(nonatomic) BOOL on;
2.代碼例子
創(chuàng)建了一個開啟狀態(tài)時顏色為藍(lán)色的控件送粱,并添加了一個可以輸出開關(guān)當(dāng)前狀態(tài)的事件世舰。
- (void)viewDidLoad {
[super viewDidLoad];
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(50, 150, 200, 50)];
sw.onTintColor = [UIColor blueColor];
[sw addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:sw];
}
-(void)test:(UISwitch *)sender{
NSLog(@"%d",sender.on);
}
關(guān)閉狀態(tài)
開啟狀態(tài)