微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 分段选择控件UISegmentedControl的用法

1,选择控件的创建,并监听控件选择值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import UIKit
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
//选项除了文字还可以是图片
let items=[ "选项一" , "选项二" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, UIImage (named: "star" )!] as [ AnyObject ]
segmented= UISegmentedControl (items:items)
segmented.center= self .view.center
segmented.selectedSegmentIndex=1 //认选中第二项
segmented.addTarget( ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,action: "segmentDidchange:" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
forControlEvents: UIControlEvents . ValueChanged ) //添加值改变监听
.view.addSubview(segmented)
}
segmentDidchange(segmented: ){
//获得选项的索引
print (segmented.selectedSegmentIndex)
//获得选择的文字
(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
}
}

2,选择控件中选项的添加删除
6
//添加文字选项
segmented.insertSegmentWithTitle( "新增选项" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,atIndex:1,animated: true );
//添加图片选项
segmented.insertSegmentWithImage( "icon" )!,monospace!important; min-height:inherit!important">)
//移除选项
segmented.removeSegmentAtIndex(1,monospace!important; min-height:inherit!important">);
3,修改选项颜色(包括图片选项)
1
segmented.tintColor= UIColor .redColor()
4,修改选项文字
segmented.setTitle("swfit"ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forSegmentAtIndex:1)
5,修改选项图片
segmented.setimage(),forSegmentAtIndex:2)
6,修改选项内容偏移位置
segmented.setContentOffset(CGSizeMake(10,7),forSegmentAtIndex:1)

新版swift中 println 已经改成 print了。获取segment选择标题时:segmented.titleForSegmentAtIndex( segmented.selectedSegmentIndex ) 返回 optional,需要加个感叹号。(Xcode7)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐