UISwitch詳解

一、UISwithch屬性說明:

tintColor:
開關(guān)處于關(guān)閉狀態(tài)時邊框的顏色(注意這邊是邊框的顏色)
onTintColor:
開關(guān)處于開啟狀態(tài)時的顏色
thumbTintColor:
開關(guān)的狀態(tài)鈕顏色
onImage:
開關(guān)處于開啟狀態(tài)時的圖片(iOS7及之后設(shè)置無效)
offImage:
開關(guān)處于關(guān)閉狀態(tài)時的圖片(iOS7及之后設(shè)置無效)
backgroundColor:整個開關(guān)背景色,設(shè)置后可以明顯看到一個矩形背景

iOS系統(tǒng)內(nèi)置了UISwithch控件的size,所以通過代碼調(diào)整UISwithch的大小無效.

默認(rèn)大小 51.0f 31.0f

二、調(diào)整UISwitch 關(guān)閉狀態(tài)背景色

tintColor屬性 只能調(diào)整off狀態(tài)的邊框顏色像這樣:

-(UISwitch *)switchFunc{
    if(_switchFunc == nil){
        _switchFunc = [[UISwitch alloc]init];
        [_switchFunc setTintColor:HEXCOLOR(0x99999)];
        [_switchFunc setOnTintColor:HEXCOLOR(kBlueColor)];
        [_switchFunc setThumbTintColor:[UIColor whiteColor]];
        _switchFunc.layer.cornerRadius = 15.5f;
        _switchFunc.layer.masksToBounds = YES;
        [_switchFunc addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchFunc;
}

結(jié)果就像這樣

開啟狀態(tài)
關(guān)閉狀態(tài)

方法:
1.設(shè)置switch的背景色



2.設(shè)置圓邊角



細(xì)看你會發(fā)現(xiàn)右邊多了點(diǎn),和我們要的效果不一樣
3.調(diào)整控件大小
49.0f, 31.0f
最終效果圖

OK
下面是核心代碼:

-(UISwitch *)switchFunc{
    if(_switchFunc == nil){
        _switchFunc = [[UISwitch alloc]init];
        [_switchFunc setBackgroundColor:HEXCOLOR(0x999999)];
        [_switchFunc setOnTintColor:HEXCOLOR(kBlueColor)];
        [_switchFunc setThumbTintColor:[UIColor whiteColor]];
        _switchFunc.layer.cornerRadius = 15.5f;
        _switchFunc.layer.masksToBounds = YES;
        [_switchFunc addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchFunc;
}

    [_switchFunc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(49.0f, 31.0f));
        make.right.mas_equalTo(self.contentView).with.offset(-12.0f);
    }];

三、調(diào)整UISwitch 大小

引用如何設(shè)置UISwitch的大小

setFrame 方法并不能更改它的大小。

UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(200, 15, 50, 15)];
     sw.transform = CGAffineTransformMakeScale( 0.5, 0.5);//縮放 
1.1 CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
這個方法可以方便的對view的長和寬進(jìn)行縮放,不改變view的中心點(diǎn)。注意!中心點(diǎn)不變指的是物理位置不變,不是坐標(biāo),因?yàn)樽鴺?biāo)系此時已經(jīng)發(fā)生改變

1.2 CGAffineTransformScale(CGAffineTransform t,CGFloat sx, CGFloat sy)
這個方法同樣是view的長和寬進(jìn)行縮放,效果類似CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)不同的是這個方法可以疊加其他CGAffineTransform效果(比如旋轉(zhuǎn))

第三方庫

一個用圖片實(shí)現(xiàn)的switch庫,動態(tài)效果相對系統(tǒng) 差點(diǎn),可定制性高
TTSwitch

一個比較有味的第三方庫
LLSwitch

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,724評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,026評論 4 61
  • 繼基礎(chǔ)控件UISlider之后,期待的UISwitch詳細(xì)介紹-->>保證你有意外收獲,如有問題歡迎指點(diǎn)。。 1、...
    MrBrave丶彬彬閱讀 4,178評論 0 0
  • 記得05年有一部韓劇叫《我的女孩》,片中由李多海扮演的女主角經(jīng)常梳著一個丸子頭,非??蓯郏瑒≈械娜菓僖部胺Q經(jīng)典。...
    緹子縈閱讀 979評論 2 4
  • 時間:2016.12.22 晚 晴 星幣騎士(感情現(xiàn)狀) 案主:感覺他準(zhǔn)備來迎娶我 我;確實(shí)有點(diǎn)像,紅色的比較喜慶...
    是小白啊閱讀 397評論 0 1

友情鏈接更多精彩內(nèi)容