let segment = UISegmentedControl(frame: CGRectMake(10.0,10.0,(CGRectGetWidth(self.view.bounds) - 10.0 * 2),40.0)) self.view.addSubview(segment) segment.backgroundColor = UIColor.lightGrayColor()
// 设置按钮(图标样式) segment.insertSegmentWithImage(UIImage(named: "image01"),atIndex: 0,animated: true) segment.insertSegmentWithImage(UIImage(named: "image02"),atIndex: 1,animated: true) segment.insertSegmentWithImage(UIImage(named: "image03"),atIndex: 2,animated: true) segment.insertSegmentWithImage(UIImage(named: "image04"),atIndex: 3,animated: true) segment.insertSegmentWithImage(UIImage(named: "image05"),atIndex: 4,animated: true) // 移除按钮 segment.removeAllSegments() // 设置按钮(标题样式) segment.insertSegmentWithTitle("红色",animated: true) segment.insertSegmentWithTitle("绿色",animated: true) segment.insertSegmentWithTitle("白色",animated: true) segment.insertSegmentWithTitle("橙色",animated: true) segment.insertSegmentWithTitle("segmentControl",animated: true) segment.insertSegmentWithImage(UIImage(named: "image05"),atIndex: 5,animated: true)
// 设置某个按钮宽度 segment.setWidth(110.0,forSegmentAtIndex: 4) // 设置某个按钮不可用 segment.setEnabled(false,forSegmentAtIndex: 4) segment.setEnabled(true,forSegmentAtIndex: 5) // 设置后没有选中高亮效果,默认false segment.momentary = false // 初始化选中按钮,默认0 segment.selectedSegmentIndex = 1 // 按钮边框及字体颜色 segment.tintColor = UIColor.greenColor() // 响应事件 segment.addTarget(self,action: Selector("segmentValueChange:"),forControlEvents: UIControlEvents.ValueChanged)
原文链接:https://www.f2er.com/swift/322904.html