前言

	NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISwitch : UIControl <NSCoding>
@available(iOS 2.0, *) public class UISwitch : UIControl, NSCoding

1、Switch 的创建

  • Objective-C

    	// 实例化 switch 对象,switch 的大小是由系统固定的
    UISwitch *switch1 = [[UISwitch alloc] init]; // 将 sw 添加到 view
    [self.view addSubview:switch1];
  • Swift

    	// 实例化 switch 对象,switch 的大小是由系统固定的
    let switch1:UISwitch = UISwitch() // 将 sw 添加到 view
    self.view.addSubview(switch1)

2、Switch 的设置

  • Objective-C

    	// 设置位置
    switch1.center = self.view.center; // 设置 tag 值
    switch1.tag = 100; // 设置外边框颜色
    switch1.tintColor = [UIColor redColor]; // 设置滑块的颜色
    switch1.thumbTintColor = [UIColor blueColor]; // 设置 on 时的颜色
    /*
    默认为绿色
    */
    switch1.onTintColor = [UIColor orangeColor]; // 设置当前的开关状态
    switch1.on = YES; // 获取当前的开关状态
    BOOL isOn = switch1.isOn; // 添加点击触发事件
    [switch1 addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
  • Swift

    	// 设置位置
    switch1.center = self.view.center // 设置 tag 值
    switch1.tag = 100 // 设置外边框颜色
    switch1.tintColor = UIColor.redColor() // 设置滑块的颜色
    switch1.thumbTintColor = UIColor.blueColor() // 设置 on 时的颜色
    /*
    默认为绿色
    */
    switch1.onTintColor = UIColor.orangeColor() // 设置当前的开关状态
    switch1.on = true // 获取当前的开关状态
    let isOn:Bool = switch1.on // 添加点击触发事件
    switch1.addTarget(self, action: #selector(UiSwitch.switchClick(_:)), forControlEvents: .ValueChanged)

3、Storyboard 中设置

  • 在 Storyboard 场景中设置

    • Switch 设置

      State 开关状态
        			  |

      On Tint | 开关开时的颜色

      Thumb Tint | 开关滑块的颜色

      |

      On Image | 开关开时的图片

      Off Image | 开关关时的图片

    • Control 设置

      Alignment 文字对齐方式
        			    |

      Content |

      -- Selected | 选中

      -- Enable | 可用

      -- Highlighted | 高亮

iOS - UISwitch的更多相关文章

  1. IOS UISwitch控件的基本使用

    * UISwitch继承自UIControl,因此也能像UIButton一样监听一些事件,比如状态改变事件* UISwitch可以通过拖线监听状态改变* UISwitch可以通过addTarget:. ...

  2. IOS UISwitch 控件

    转自:http://my.oschina.net/wolx/blog/396680 一 UISwitch 简介 二 UISwitch 创建 三设置选中状态 四 添加监听 五 测试代码 5.1 代码 5 ...

  3. IOS 7 开发范例 - UISwitch的使用

    Creating and Using Switches with UISwitch You would like to give your users the ability to turn an o ...

  4. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  5. IOS开发UI基础UISwitch属性

    UISwitch属性1. onTintColor   处于on时switch 的颜色
    switchImage.onTintColor = [UIColor grayColor];2.tintC ...

  6. iOS开发——UI篇Swift篇&UISwitch/UIStepper

    UISwitch/UIStepper override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...

  7. iOS - UI - UISwitch

    UISwitch //开关    不用设置宽高  有默认宽高 UISwitch * sw = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100,  ...

  8. UISwitch + UIimage - 初识IOS

    这里解释一个小例子,希望对你有点帮助,利用UISwitch控制UIimage的动画效果 先定义一个数组,用来存放照片,现在定义数组有一个特别简单的方法: NSArray *image1 = @[]; ...

  9. IOS开发之XCode学习011:UISwitch控件

    此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能:  1.定义UIswitch控件,添加UIswitc ...

随机推荐

  1. nohup DEMO

    nohup,顾名思义:挂起免疫. nohup命令可以防止当你退出系统时,在后台运行的进程被终止.它能让你运行的命令或脚本在你退出系统后继续在后台运行. nohup命令不能自动的将任务放在后台运行,所以 ...

  2. SQL中char、varchar、nvarchar的区别

    char    char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.   nvarcha ...

  3. symfony中twig的模板过滤器

    过滤器 变量可以被过滤器修饰.过滤器和变量用(|)分割开.过滤器也是可以有参数的.过滤器也可以被多重使用. 通用过滤器 date过滤器 1.1版本新增时区支持,1.5版本增加了默认的日期格式.格式化时 ...

  4. PHP简单图片操作

    <?php //PHP操作图片需打开配置文件中 extension=php_gd2.dll //================================================= ...

  5. asp.net 分页-利用后台直接生成html分页

    一直想做一个属于自己的分页, 1:我试过用datapage,虽然是很好用,但是必须要配合datalist才能使用,感觉不太好 2:自己写分页控件,目前正在摸索中,关键是怎么分页的问题,有的是用data ...

  6. Proud Merchants

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) To ...

  7. python请求java Selenium Webdriver

    下载jar包: selenium-server-standalone-2.44.0.jar 运行jar包: java -jar selenium-server-standalone-2.44.0.ja ...

  8. gcc/g++动态链接库和静态库的链接顺序

    转自:http://withc8212.blog.163.com/blog/static/11656983820109263562854/ so文件:动态库a文件: 静态库exe文件:可执行程序(li ...

  9. linux ftp服务

    1 安装ftp服务 [root@localhost ~]# yum install vsftpd 启动:service vsftpd start 查看状态:systemctl |grep vsftpd ...

  10. log4j---------学习总结(一)

    一.log4j框架 log4j可靠的,快速的,可扩展的(灵活的)日志框架 log4j具有高可配置性,可以通过外部配置文件来控制 log4j可以根据日志不同级别来过滤日志, log4j可以将日志记录直接 ...