UISwitch的使用
2016-01-26 00:00
483 查看
开发中设置界面一般会用到的开关控件。比如飞行模式开关,wifi开关。
UISwitch继承于UIControl,有addTarget方法增加其事件。代码创建:
属性设置:
1、onTintColor设置开启颜色;
2、onImage设置开启图片;
3、tintColor设置正常关闭颜色;
4、offImage设置关闭图片;
5、thumbTintColor设置圆形按钮颜色;
代码如下:
关于设置开启/关闭状态和 获取UISwitch对象的开启/关闭状态。
点击事件,事件的处理也是根据UISwitch对象的开启/关闭状态来区分的。
UISwitch继承于UIControl,有addTarget方法增加其事件。代码创建:
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 300, 50)]; // mySwitch.backgroundColor = [UIColor orangeColor];//背景颜色,一般不用,不好看。 [self.view addSubview:mySwitch];
属性设置:
1、onTintColor设置开启颜色;
2、onImage设置开启图片;
3、tintColor设置正常关闭颜色;
4、offImage设置关闭图片;
5、thumbTintColor设置圆形按钮颜色;
代码如下:
//设置开启颜色 图片 mySwitch.onTintColor = [UIColor yellowColor]; mySwitch.onImage = [UIImage imageNamed:@""]; //设置关闭颜色 图片 mySwitch.tintColor = [UIColor redColor];//设置正常关闭颜色 mySwitch.offImage = [UIImage imageNamed:@""]; //设置圆形按钮颜色 mySwitch.thumbTintColor = [UIColor purpleColor];
关于设置开启/关闭状态和 获取UISwitch对象的开启/关闭状态。
//代码设置开启、关闭状态 设置YES或者NO,是否使用动画效果 [mySwitch setOn:YES animated:YES]; //获取UISwitch的开启/关闭状态 获取对象的isOn属性,默认是关闭状态 如果isOn==YES则是开启状态,如果isOn==NO则是关闭状态。 if (mySwitch.isOn) { NSLog(@"开启状态"); } else { NSLog(@"关闭状态"); }
点击事件,事件的处理也是根据UISwitch对象的开启/关闭状态来区分的。
[mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged]; -(void)switchAction:(id)sender { UISwitch *switchButton = (UISwitch*)sender; BOOL isButtonOn = [switchButton isOn]; if (isButtonOn) { // 开启状态的处理 }else { //关闭状态的处理 } }
相关文章推荐
- UIAlertView的基本使用
- 修改iOS UITableView分割线左边短一截
- iOS开发-UIScrollView的用法
- UILabel的使用
- qq聊天界面,等我研究明白怎么附加文件夹到。。
- UITextView的输入图标不是从开头开始
- RabbitMQ消息分发模式----"Work Queues"模式
- When encountered a program hang,what command can I use to analysis this issue
- [读书笔记]C#学习笔记八:StringBuilder与String详解及参数传递问题剖析
- UIView没有暴露出来的CALayer功能
- 从grub rescue> 问题解决,温习操作系统中文件系统的相关内容
- 【YouXue 1266.5】回文正整数
- UI_tableView
- Sequential projection learning for hashing阅读笔记
- Repeated DNA Sequences
- 设置UILabel控件的圆角
- WWDC15 Session笔记 - Xcode 7 UI 测试初窥
- PyQuery查询html信息
- 从后台绑定数据到ligerui 的comboBox下拉框组件
- mysql的Invalid default value for 'timestamp'"错误解决