国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS 開關(guān)-UISwitch

2019-11-09 16:40:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

正如分段控件代替了單選按鈕,開關(guān)也代替了點(diǎn)選框,一般來(lái)說(shuō)我的和設(shè)置頁(yè)面經(jīng)常常需要這種開關(guān)的需求,我們就來(lái)看看吧!

首先介紹一下switch控件

switch在UIKit框架之下,繼承自UIControl,可以添加觸發(fā)事件。開關(guān)狀態(tài)下默認(rèn)的樣式如下. 開狀態(tài)下的樣式 關(guān)狀態(tài)下的樣式

點(diǎn)進(jìn)去UISwitch,可以發(fā)現(xiàn)switch有以下的屬性和方法:

屬性:

onTintColor UIColor 開狀態(tài)下的顏色tintColor UIColor 關(guān)狀態(tài)下的顏色thumbTintColor UIColor 滑塊顏色onImage UIImage 無(wú)效offImage UIImage 無(wú)效on( isOn) BOOL isOn是用來(lái)獲取狀態(tài)的是get方法,on可以用來(lái)設(shè)置開關(guān)

方法:

- (instancetype)initWithFrame:(CGRect)frame; // This class enforces a size apPRopriate for the control, and so the frame size is ignored.它有默認(rèn)size,因此frame的size可以忽略,即使你定制了size系統(tǒng)也會(huì)忽略掉。- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;- (void)setOn:(BOOL)on animated:(BOOL)animated; // does not send action??:雖然系統(tǒng)默認(rèn)switch的size,但是switch的size也不是不能變的,我們可以通過(guò)縮放來(lái)改變switch的大小,如果還是不能滿足你的需求,那你就需要自定義啦!

Objective-C代碼:

//2. create switch self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 70, 0, 0)]; [self.view addSubview:self.mainSwitch]; //縮小或者放大switch的size self.mainSwitch.transform = CGAffineTransformMakeScale(0.5, 0.5); self.mainSwitch.layer.anchorPoint = CGPointMake(0, 0.3);// self.mainSwitch.onImage = [UIImage imageNamed:@"on.png"]; //無(wú)效// self.mainSwitch.offImage = [UIImage imageNamed:@"off.png"]; //無(wú)效// self.mainSwitch.backgroundColor = [UIColor yellowColor]; //它是矩形的 // 設(shè)置開關(guān)狀態(tài)(默認(rèn)是 關(guān))// self.mainSwitch.on = YES; [self.mainSwitch setOn:YES animated:true]; //animated //判斷開關(guān)的狀態(tài) if (self.mainSwitch.on) { NSLog(@"switch is on"); } else { NSLog(@"switch is off"); } //添加事件監(jiān)聽 [self.mainSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; //定制開關(guān)顏色UI //tintColor 關(guān)狀態(tài)下的背景顏色 self.mainSwitch.tintColor = [UIColor redColor]; //onTintColor 開狀態(tài)下的背景顏色 self.mainSwitch.onTintColor = [UIColor yellowColor]; //thumbTintColor 滑塊的背景顏色 self.mainSwitch.thumbTintColor = [UIColor blueColor];

swift代碼:

//2. create switch mainSwitch = UISwitch(frame: CGRect(x: 100, y: 100, width: 0, height: 0)) view.addSubview(self.mainSwitch) //縮小或者放大switch的size mainSwitch.transform = CGAffineTransform(scaleX: 0.8, y: 0.8) mainSwitch.layer.anchorPoint = CGPoint(x:0, y:0.3) mainSwitch.onImage = UIImage(named:"on.png") //無(wú)效 mainSwitch.offImage = UIImage(named:"off.png") //無(wú)效// mainSwitch.backgroundColor = UIColor.yellow //設(shè)置背景顏色之后才發(fā)現(xiàn)原來(lái)它是矩形的 // 設(shè)置開關(guān)狀態(tài)(默認(rèn)是 關(guān))// self.mainSwitch.isOn = true; mainSwitch.setOn(true, animated: true) // animated //判斷switch的開關(guān)狀態(tài) if mainSwitch.isOn { print("switch is on") } else { print("switch is off") } //添加監(jiān)聽事件 mainSwitch.addTarget(self, action: #selector(switchAction), for: .valueChanged) //定制開關(guān)顏色UI //tintColor 關(guān)狀態(tài)下的背景顏色 mainSwitch.tintColor = UIColor.red; //onTintColor 開狀態(tài)下的背景顏色 mainSwitch.onTintColor = UIColor.yellow; //thumbTintColor 滑塊的背景顏色 mainSwitch.thumbTintColor = UIColor.blue;

當(dāng)然,有的項(xiàng)目里的開關(guān)UI需要自定義字體或者自定義圖片,那么就需要自己自定義switch啦!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万宁市| 宁明县| 宁津县| 司法| 潼关县| 龙江县| 昌平区| 西华县| 当雄县| 大悟县| 获嘉县| 正定县| 文水县| 弋阳县| 临江市| 满洲里市| 静安区| 固安县| 抚远县| 图木舒克市| 遂溪县| 大石桥市| 济源市| 阿合奇县| 建瓯市| 海南省| 蒙山县| 麦盖提县| 满城县| 巴林左旗| 南平市| 视频| 富川| 曲松县| 南郑县| 方城县| 海伦市| 华安县| 如皋市| 罗田县| 云浮市|