iOS - UISwitch
前言
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的更多相关文章
- IOS UISwitch控件的基本使用
* UISwitch继承自UIControl,因此也能像UIButton一样监听一些事件,比如状态改变事件* UISwitch可以通过拖线监听状态改变* UISwitch可以通过addTarget:. ...
- IOS UISwitch 控件
转自:http://my.oschina.net/wolx/blog/396680 一 UISwitch 简介 二 UISwitch 创建 三设置选中状态 四 添加监听 五 测试代码 5.1 代码 5 ...
- IOS 7 开发范例 - UISwitch的使用
Creating and Using Switches with UISwitch You would like to give your users the ability to turn an o ...
- iOS自定义的UISwitch按钮
UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...
- IOS开发UI基础UISwitch属性
UISwitch属性1. onTintColor 处于on时switch 的颜色 switchImage.onTintColor = [UIColor grayColor];2.tintC ...
- iOS开发——UI篇Swift篇&UISwitch/UIStepper
UISwitch/UIStepper override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...
- iOS - UI - UISwitch
UISwitch //开关 不用设置宽高 有默认宽高 UISwitch * sw = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, ...
- UISwitch + UIimage - 初识IOS
这里解释一个小例子,希望对你有点帮助,利用UISwitch控制UIimage的动画效果 先定义一个数组,用来存放照片,现在定义数组有一个特别简单的方法: NSArray *image1 = @[]; ...
- IOS开发之XCode学习011:UISwitch控件
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.定义UIswitch控件,添加UIswitc ...
随机推荐
- webservice cxf error:java.lang.IllegalArgumentException: Argument(s) "type" can't be null.
客户端请求DTO和服务器端的DTO定义不一样,客户端必须定义@XmlAccessorType和@XmlType,如: @XmlAccessorType(XmlAccessType.FIELD) @Xm ...
- XCode5 真机调试及发布应用
一.XCODE 真机测试 Xcode5已经很智能,只需生成一个开发证书,安装后,插入设备会自动添加,注意,当Mac系统升级后,证书需要重新生成. 证书生成步骤: 1.生成 CertificateSig ...
- 【PHP设计模式 01_DuoTai.php】多态的说明
<?php /** * [多态] * 定义一个抽象类:Tiger,有两个子类:XTiger 和 MTiger */ header("Content-type: text/html; c ...
- PHP学习当中遗漏的知识点
一, 当双引号中包含变量时,变量会与双引号中的内容连接在一起: 当单引号中包含变量时,变量会被当做字符串输出. <?php $love = "I love you!"; $s ...
- window下appserv组合包配置asp标记风格与简短风格
php一共有四种编码风格 分别为 :XML风格,脚本分铬,简短风格,ASP风格 如果要配置asp标记风格与简短风格,需要在php.ini文件中配置. 打开文件的位置C:\ window\php.ini ...
- YTU 3005: 皇后问题(栈和队列)
3005: 皇后问题(栈和队列) 时间限制: 1 Sec 内存限制: 128 MB 提交: 6 解决: 3 题目描述 编写一个函数,求解皇后问题:在n*n的方格棋盘上,放置n个皇后,要求每个皇后不 ...
- poj1703 Find them, Catch them
并查集. 这题错了不少次才过的. 分析见代码. http://poj.org/problem?id=1703 #include <cstdio> #include <cstring& ...
- Uva 10305 给任务排序
题目链接:https://uva.onlinejudge.org/external/103/10305.pdf 紫书P167 拓扑排序. dfs——从一个点出发,dfs 与之相连的所有点,把本身放入到 ...
- HDU(4734),数位DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4734 F(x) Time Limit: 1000/500 MS (Java/Others) ...
- lua注释
1. 单行注释 -- 功能等同于C++中的// 2. 多行注释 --[[ 注释的内容 ]] 功能等同于C++中的 /**/ 3. 多行注释 --[====[ 注释和内容 ]=== ...