UIButton的创建:
(1) 在Stroyboard中使用Ctrl+Drag拖拽法创建
(2) 代码创建:UISegSmentedControl
【Swift代码】 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //设置选项的内容 let segItems = ["头条","科技","财经","历史"]; //初始化选项卡控件(分段选择控件) let segmentMenu:UISegmentedControl = UISegmentedControl(items: segItems); segmentMenu.center = self.view.center; segmentMenu.selectedSegmentIndex = 0; //默认选择第一个选项 //添加事件 segmentMenu.addTarget(self, action: "segmentDidChanged:", forControlEvents: UIControlEvents.ValueChanged ); //添加子视图 self.view.addSubview(segmentMenu); } func segmentDidChanged(segmentMenu:UISegmentedControl){ //获得选项的索引 print(segmentMenu.selectedSegmentIndex) //获得选择的文字 print(segmentMenu.titleForSegmentAtIndex(segmentMenu.selectedSegmentIndex)!) //修改控件的颜色 segmentMenu.tintColor=UIColor.greenColor() //修改控件的背景颜色 //segmentMenu.backgroundColor = UIColor.grayColor() }
运行环境:Xcode Version 7.0 (7A220)
参考资料:
http://www.hangge.com/blog/cache/detail_533.html
http://www.cnblogs.com/li--nan/p/4506826.html
时间: 2024-10-26 23:55:09