国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 东兰县| 唐河县| 晋江市| 都昌县| 石狮市| 宁河县| 温宿县| 鹿邑县| 县级市| 五原县| 海兴县| 武强县| 温泉县| 普格县| 万盛区| 石林| 田东县| 凤凰县| 汝阳县| 卓资县| 长沙市| 翁牛特旗| 兴国县| 师宗县| 南郑县| 河北省| 丰宁| 新疆| 山阳县| 营山县| 朝阳市| 汉沽区| 鄯善县| 西吉县| 德清县| 夏河县| 江都市| 西林县| 青河县| 绥德县| 珠海市|