Swift-use of segmented selection controls (Uisegmentedcontrol)-Basic usage

Source: Internet
Author: User

1, select the control's creation and listen for the control selection value 1 2 3 4 5 6 7 8 9 import  uikit   class  V Iewcontroller:  uiviewcontroller  {       override  func  viewdidload () {         super. Viewdidload ()                    //options In addition to text can also be pictures           let  items=["option One", "option Two", UIImage (named:  "star")!]   as  [Anyobject]          let  segmented= Uisegmentedcontrol (items:items)          segmented.center= self. View.center           segmented.selectedsegmentindex=1 ///default Select the second item           Segmented.addtarget (self, action:  "Segmentdidchange:",              forcontrolevents:  uicontrolevents. valuechanged)   //Add Value change monitor          self. View.addsubview ( segmented)     }            func  Segmentdidchange (Segmented:uisegmentedcontrol) {        //Get Options Index           print (Segmented.selectedsegmentindex)          //Get selected text          print ( Segmented.titleforsegmentatindex (Segmented.selectedsegmentindex))     }}
2, select the selection of the control add and remove 1 2 3 4 5 6//Add Text Options Segmented.insertsegmentwithtitle ("New Options", atindex:1,animated:true); Add Picture Options Segmented.insertsegmentwithimage (UIImage (named: "icon")!,atindex:1,animated:true)//Removal options Segmented.removesegmentatindex (1,animated:true);
3, modify the option color (including picture options) 1 segmented.tintcolor= uicolor. Redcolor ()
4, modify 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 location 1 segmented.setcontentoffset (Cgsizemake (10,7), forsegmentatindex:1)
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.

Tags Index: