UISwitch

96
无邪8
2018.05.23 11:38 字数 363

一、简介
<<UISwitch 的作用是给用户提供开关,在系统的设置界面很常见

<<继承关系:UISwitch-->UIControl-->UIView-->UIResponder-->NSObject

格式为

1-->设置开状态下的颜色(作用)

switch.onTintColor = [UIColor yellowColor]; (这是具体的例子)

@property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;(这是说明)

二、UISwitch的属性(属性的顺序与苹果API一致)

1--> 设置开状态下的颜色

switch.onTintColor = [UIColor yellowColor];

@property(nullable, nonatomic, strong) UIColor *onTintColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

2-->设置关状态下的背景颜色

switch.tintColor = [UIColor redColor];

@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(6_0);

3-->设置滑块的背景颜色

switch.thumbTintColor = [UIColor blueColor];

@property(nullable, nonatomic, strong) UIColor *thumbTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;

4-->设置开关处于开启状态时的图片(iOS7及之后设置无效)

switch.onImage = [UIImage imageNamed:@"on.png"];

@property(nullable, nonatomic, strong) UIImage *onImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
5-->设置开关处于关闭状态时的图片(iOS7及之后设置无效)

switch.offImage = [UIImage imageNamed:@"off.png"];

@property(nullable, nonatomic, strong) UIImage *offImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;
6--> 设置switch的开关

swith.on = YES;

@property(nonatomic,getter=isOn) BOOL on;

7-->初始化方法

UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 20, 10)];

-(instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;
8-->初始化方法

具体参看Objective-c 中如何重写父类的初始化方法

-(nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
9-->设置开关状态

[switch2 setOn:YES animated:YES]

-(void)setOn:(BOOL)on animated:(BOOL)animated;

ios-UIResponder开发控件