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 ...
随机推荐
- Extended Data Type Properties [AX 2012]
Extended Data Type Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: ...
- 13、mysql/触发器
1. mysql mysql基础 1)mysql存储结构: 数据库 -> 表 -> 数据 sql语句 2)管理数据库: 增加: create database 数据库 default ...
- Python基础第一篇
一.第一句python代码 1.python执行过程:1.加载内存-词法分析-语法分析-编译-执行 2.创建hello.py文件,输入内容 #!/usr/bin/env python print &q ...
- 使用compile_scripts.php脚本,生成lua打包的zip,解决加密问题
@echo off set DIR=%~dp0 set TEMPLATE_ROOT=%DIR%.. echo %TEMPLATE_ROOT%\quick\bin\win32\php.exe echo ...
- DelegateCommand.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- TreeView 节点的显示,读取,操作
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq ...
- CSUFT 1004 This is Halloween: Saving Money
1004: This is Halloween: Saving Money Time Limit: 1 Sec Memory Limit: 128 MB Submit: 11 So ...
- linux下查看电脑配置
1. 查看cpu ~$ cat /proc/cpuinfo 2. 查看内存占用 ~$ cat /proc/meminfo 3. 硬盘分区 $ cat /proc/partitions 4. ubunt ...
- bzoj 2818: Gcd GCD(a,b) = 素数
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1566 Solved: 691[Submit][Status] Descript ...
- ubuntu 安装 Tomcat
首先确认系统中没有安装openJDK,有的话先卸载,安装oracle jdk 下载jdk Linux x64 172.95 MB jdk-8u101-linux-x64.tar.gz ...