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 高亮
-

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
iOS - UIButton
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIButton : UIControl <NSCoding> @available(iOS 2.0, *) public class UIButton : UIControl, NSCoding 1、UIButton 的创建 Objective-C // 实例化 button 对象 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // 将 button 添加到 window [self.view addSubview:button]; Swift // 实例化 button 对象 let button:UIButton = UIButton(type: .Custom) // 将 button 添加到 window self.view.addSubview(button) 2、UIButton 的设置 Objective-C // 设置 button 类型 /* UIButtonTypeCustom = 0,...
- 下一篇
iOS - UISegmentedControl
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UISegmentedControl : UIControl <NSCoding> @available(iOS 2.0, *) public class UISegmentedControl : UIControl, NSCoding UISegmentedControl 的 Items 有两种样式,一种是字符串设置的标题样式,一种是图片设置的样式。 图片设置的样式需要处理图片的渲染模式,以原图显示。处理阴影,如果直接设置 image 出现阴影,这时候需要设置图片的渲染模式以原样显示。 UIImage *image = [UIImage imageNamed:@"hehe.png”]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 1、UISegmentedControl 的创建 Objective-C // 不设置 frame 时为系统默认大小 UISegmentedControl *...
相关文章
文章评论
共有0条评论来说两句吧...