One, create the control, and listen to the control selection value
/*options In addition to the text can also be the image as the keyword function is literally: type conversion*///Let items = ["Option One", "option Two", UIImage (named: "Hgl.jpeg")!] as [Anyobject]Let items = ["Option One","option two"] Let segmented=Uisegmentedcontrol (Items:items)//Segmented.center = Self.view.centerSegmented.frame = CGRectMake ( the,410, Screen_width- the, -) Segmented.selectedsegmentindex=1 //the second item is selected by defaultSegmented.addtarget (Self, Action: #selector (Segmentedclick), forControlEvents:UIControlEvents.ValueChanged)//add value change listenerSelf.view.addSubview (segmented)
Implementation of the method
func Segmentedclick (segmented:uisegmentedcontrol) { // Gets the index of the option print ( Segmented.selectedsegmentindex) // Get the selected text print ( Segmented.titleforsegmentatindex (Segmented.selectedsegmentindex)) }
Second, the other properties
//Select Add and remove options in the spaceSegmented.insertsegmentwithtitle ("New Options", Atindex:2, Animated:true) //Add Picture OptionsSegmented.insertsegmentwithimage (UIImage (named:"Hgl.jpeg"), Atindex:1, Animated:true) //Removal OptionsSegmented.removesegmentatindex (1, Animated:true) //Modify the color of the options (including picture options)Segmented.tintcolor =Uicolor.redcolor ()//Modify Options text/PictureSegmented.settitle ("Swift", Forsegmentatindex:2) Segmented.setimage (UIImage (named:"Hgl.jpeg"), Forsegmentatindex:2) //Modify Option content offset positionSegmented.setcontentoffset (Cgsizemake (5,3), Forsegmentatindex:2)
The use of Swift-uisegmentedcontrol