iOS-UIKit框架学习—UISwitch

您可以使用UISwitch类来创建和管理ON/ OFF按钮,您会看到,例如在飞行模式等服务的偏好设定(设定)。这些对象被称为开关。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISwitch : UIControl <NSCoding>
// 开关开启时的颜色 On
@property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
// 背景边框颜色
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(6_0);
// 开关关闭时的颜色 Off
@property(nullable, nonatomic, strong) UIColor *thumbTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
// 打开时的状态图片
@property(nullable, nonatomic, strong) UIImage *onImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
// 关闭时的状态图片
@property(nullable, nonatomic, strong) UIImage *offImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
// 开关
@property(nonatomic,getter=isOn) BOOL on;
// 初始化
- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
// 设置开关并有动画效果
- (void)setOn:(BOOL)on animated:(BOOL)animated;
@end

e.g.

1
2
3
4
5
6
UISwitch *switch1 = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];
switch1.onTintColor = [UIColor redColor];
switch1.thumbTintColor = [UIColor blueColor];
switch1.tintColor = [UIColor yellowColor];
[switch1 setOn:YES animated:YES];
[self.view addSubview:switch1];