加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

UIKit框架-基础控件Swift版本: 7.UISwitch方法/属性详解

发布时间:2020-12-14 02:14:36 所属栏目:百科 来源:网络整理
导读:前面我们讲解完了 iOS 中的几个基础控件,现在让我们继续来看其他基础控件. 1.UISwitch的常用属性 // 1.设置 UISwitch 打开时底图渐变颜色,默认是从白变绿 var onTintColor: UIColor! // 2.设置 UISwitch 关闭时底图渐变颜色,默认是从白变绿 var tintColor: U

前面我们讲解完了 iOS 中的几个基础控件,现在让我们继续来看其他基础控件.


1.UISwitch的常用属性

// 1.设置 UISwitch 打开时底图渐变颜色,默认是从白变绿
    var onTintColor: UIColor!

// 2.设置 UISwitch 关闭时底图渐变颜色,默认是从白变绿 
    var tintColor: UIColor?

// 3.设置 UISwitch 的小圆点颜色,默认是白色
    var thumbTintColor: UIColor?

// 4.设置 UISwitch 是否打开
    var on: Bool

// 5.设置 UISwitch 的位置
    init(frame: CGRect)

// 6.设置 UISwitch 是否打开,并且是否打开动画效果
    func setOn(on: Bool,animated: Bool)

2.代码演示

自定义UISwitch

func mySwitch() {
        // 1.自定义 UISwitch
        var switchButton = UISwitch()

        // 2.设置 UISwitch 的位置
        switchButton.center = CGPointMake(self.view.frame.width / 2,self.view.frame.height / 2)

        // 3.设置 UISwitch 打开时底图渐变颜色,默认是从白变绿
        switchButton.onTintColor = UIColor.redColor()

        // 4.设置 UISwitch 关闭时底图渐变颜色,默认是从绿变白
        switchButton.tintColor = UIColor.blackColor()

        // 5.设置 UISwitch 的小圆点颜色,默认是白色
        switchButton.thumbTintColor = UIColor.blueColor()

        // 6.设置 UISwitch 是否打开
        switchButton.on = true

        // 7.设置 UISwitch 是否打开,并且是否打开动画效果
        switchButton.setOn(true,animated: true)

        // 8.添加到 UISwitch 到 self.view
        self.view.addSubview(switchButton)
    }

在 viewDidLoad中实现

override func viewDidLoad() {
        super.viewDidLoad()
        self.mySwitch()
    }

3.最终效果

1

2

3

PS: UISwitch 是继承于 UIControl,所以 UIControl 里面的属性和方法 UISwitch 都是可以用的.


好了,这次我们就讲到这里,下次我们继续

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读