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 ...
随机推荐
- Verilog HDL基础语法讲解之模块代码基本结构
Verilog HDL基础语法讲解之模块代码基本结构 本章主要讲解Verilog基础语法的内容,文章以一个最简单的例子"二选一多路器"来引入一个最简单的Verilog设计文件的 ...
- CentOS下使用Percona XtraBackup对MySQL5.6数据库innodb和myisam的方法
Mysql卸载从下往上顺序 [root@localhost /]# rpm -e --nodeps qt-mysql-4.6.2-26.el6_4.x86_64[root@localhost /]# ...
- NSData NSDate NSString NSArray NSDictionary 相互转换
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...
- Codeforces 735D:Taxes(哥德巴赫猜想)
http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得 ...
- APP运营推广那点事【干货】
你的手机里面有多少应用?什么样的手机应用吸引你?下载之后经常用还是让他shi在那里?又或者刚点进去就卸载? 一款成功的应用,开发APP只是第一步,比前者更重要的是“养”APP,APP就像是一个需要不断 ...
- android 6.0权限处理
在模拟器测试好的程序,运行在mate8上面一直崩,经多方查探才找到以下博文,方法还没掌握,但也算是找到原因了: http://***/article/android-6-0-runtime-permi ...
- meta标签部分总结
<meta>标签用于提供页面的元信息,比如针对搜索引擎和更新频度的描述和关键词.由于看到很多网页<head>里面<meta>标签的内容很多,对这些标签含义了解不太清 ...
- quick-cocos2d-x 接入支付宝(android)(转,待验证)
quick-cocos2d-x 实现在lua里面完成android支付宝的接入 一.支付宝注册是很麻烦的一个过程,本文就不解释了,想了解的去官网看下注册流程.然后下载他们的sdk-WS_SECURE_ ...
- SQL游标应用
自己整了半天才弄好,写成博客纪念下: 这个是sql上写的测试用: ) ) ) ) declare @sql varchar(max) set @sql='' SET @type='index_02' ...
- android 实现橡皮擦效果以及保存涂鸦的功能
实现涂鸦.擦除.保存的功能 设置画笔为橡皮擦功能 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 如果你的画出来的是 ...