Use of the Swift-segmented selection control (Uisegmentedcontrol)

Source: Internet
Author: User

1, select the control's creation, and listen for control selection values

123456789101112131415161718192021 class ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()         //选项除了文字还可以是图片        var items=["选项一","选项二",UIImage(named:"star")] as [AnyObject]        var segmented=UISegmentedControl(items:items)        segmented.center=self.view.center        segmented.selectedSegmentIndex=1 //默认选中第二项               segmented.addTarget(self, action: "segmentDidchange:",        forControlEvents: UIControlEvents.ValueChanged//添加值改变监听          self.view.addSubview(segmented)    }    func segmentDidchange(segmented:UISegmentedControl){        //获得选项的索引        println(segmented.selectedSegmentIndex)        //获得选择的文字        println(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))    }}

2, select Add and remove items in the control

123456 //添加文字选项segmented.insertSegmentWithTitle("新增选项",atIndex:1,animated:true); //添加图片选项segmented.insertSegmentWithImage(UIImage(named:"icon")!,atIndex:1,animated: true)//移除选项segmented.removeSegmentAtIndex(1,animated:true);

3. Modify the option color (including picture options)

1 segmented.tintColor=UIColor.redColor()

4. Modify the option text

1 segmented.setTitle("swfit",forSegmentAtIndex:1)

5, modify the option picture

1 segmented.setImage(UIImage(named:"icon"),forSegmentAtIndex:2)

6, modify the option content offset position

1 segmented.setContentOffset(CGSizeMake(10,7),forSegmentAtIndex:1)

Use of the Swift-segmented selection control (Uisegmentedcontrol)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.