前言

	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. Codeforces 743D:Chloe and pleasant prizes(树形DP)

    http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...

  2. vim中设置自动匹配括号和引号

    vim ~/.vimrc 在.vimrc中添加一下几行 inoremap ( () <LEFT> inoremap { {} <LEFT> inoremap [ [] < ...

  3. [xcode]instruments来检验你的app

      原文网址:http://www.cocoachina.com/industry/20140114/7696.html     比较了好多关于instruments 还是发现老外写的比较牛逼.于是果 ...

  4. poj1222 EXTENDED LIGHTS OUT

    设输入矩阵为A,输出矩阵为B,目标矩阵为C(零矩阵). 方便起见,矩阵行列下标均从1开始. 考虑A矩阵元素a(i,j),B矩阵中与其相邻的元素 b(i,j),b(i - 1, j),b(i + 1,j ...

  5. Java中通过JDBC远程连接Oracle数据库

    通过jdbc连接数据库,拢共分三步: 第一步:下载一个JDBC的驱动,然后把jar包扔到项目里并add to build path: 第二步:去本地oracle文件夹下找到“TNSNAMES.ORA” ...

  6. 制作动画效果:《CSS3 Animation》

    在开始介绍Animation之前我们有必要先来了解一个特殊的东西,那就是"Keyframes",我们把他叫做“关键帧”,玩过flash的朋友可能对这个东西并不会陌生.下面我们就一起 ...

  7. sql server 添加字段并且赋默认值和说明

    select soct.Captcha,CreateOn,* from SceneryOrderCheckTicket soctright join (SELECT Captcha,convert(c ...

  8. lua中特殊用法

    th> a=torch.zeros(,) [.0001s] th> a [torch.DoubleTensor of size 1x5] [.0001s] th> a[{,floor ...

  9. JS中innerHTML 和innerText和value的区别

    (1)innerHTML 和innerText和value的区别: innerHTML innerText是对非表单元素进行操作的. value是对表单元素进行操作的. (2)innerHTML 和i ...

  10. out 传值

    public void Out(out int a, out int b) {//out相当于return返回值 //可以返回多个值 //拿过来变量名的时候,里面默认为空值 a=1; b=2; } s ...