class ViewController: UIViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let items = ["选项一","选项二","选项三"]
let segmented = UISegmentedControl(items: items)
segmented.center = self.view.center
segmented.selectedSegmentIndex = 1
segmented.tintColor = UIColor.red
segmented.addTarget(self, action: #selector(segmentedAction(segmented:)), for: .valueChanged)
self.view.addSubview(segmented)
}
@objc func segmentedAction(segmented:UISegmentedControl) -> Void {
NSLog("segmented - %d", segmented.selectedSegmentIndex)
}
}
segmented.setTitle("swift", forSegmentAt: 1)
segmented.setImage(UIImage.init(named: ""), forSegmentAt: 2)
segmented.setContentOffset(CGSize.init(width: 10.0, height: 7.0), forSegmentAt: 1)
segmented.insertSegment(withTitle: "Objective-C", at: 4, animated: true)
segmented.insertSegment(with: UIImage.init(named: ""), at: 5, animated: true)
segmented.removeSegment(at: 5, animated: true)
网友评论