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 ...
随机推荐
- php const define 区别有那些呢?
(1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使用. (2) 类型和安全检查不同 define宏没有类型,不做任何类型检查,仅仅是展开. const常量有 ...
- 一道面试题比较synchronized和读写锁
一.科普定义 这篇博文的两个主角“synchronized”和“读写锁” 1)synchronized 这个同步关键字相信大家都用得比较多,在上一篇“多个线程之间共享数据的方式”中也详细列举他的应用, ...
- c#调用c++的dll,错误篇
"LIPS.vshost.exe"(托管(v4.0.30319)): 已加载"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Sys ...
- okhttp封装
对这玩意并不熟,网上有很多大神封装好的,但是懒得看里面的封装逻辑,索性自己简单做个封装,方便使用,出现bug也好查找: get请求: /** * get请求 * @param url * @param ...
- 跟我一起Django - 01 安装和初始化
跟我一起Django - 01 安装和初始化 python环境 C:\Python27\Lib\site-packages>python --versionPython 2.7.5 安装setu ...
- c#xml追加读取节点
读取 if (File.Exists("Book.xml")) { XmlDocument doc = new XmlDocument(); doc.Load("Book ...
- hasOwnproperty详细总结
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员. isPrototypeO ...
- 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...
- Linux文件描述符与打开文件之间的区别(转载)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965239 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为: ...
- A fatal error has been detected by the Java Runtime Environment(jdk 1.6的一个BUG)
几天做项目,生成一堆注解的实体,当实体数超过86个时,jvm报错: # # A fatal error has been detected by the Java Runtime Environmen ...