UISegmentedControl

2019-08-18  本文已影响0人  CaptainRoy
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)
上一篇 下一篇

猜你喜欢

热点阅读