Tag: It's going to insert the picture subscript Adjust definition title Tle Event
Today, we're going to say, what does this control do with the segmented selection controller Uisegmentedcontrol in iOS?
- Each segment can be clicked, which is equivalent to integrating multiple buttons
- Usually we will click on different segment to switch different view
So what effect does it make, we look first:
This is the effect we achieve, here furniture, lighting and so on each item is a button, click will trigger different events or jump to different pages
So how to achieve this effect can be, just a few simple lines of code:
//into the data of the title nsarray *array = [ Nsarray arraywithobjects:@ "Furniture", @ "lighting", @ "Building Materials", @ "decoration", nil]; //initialization uisegmentedcontrol uisegmentedcontrol *segment = [[uisegmentedcontrol Alloc]initwithitems:array]; //set frame Segment.frame = cgrectmake ( 10, 100, Self.view.frame.size.width-20, 30); //added to view [self.view addsubview:segment];
Such a simple segmented controller is generated, which is not yet available, and we need to set it up according to its properties in actual development.
Uisegmentedcontrol Property settings
After segment is generated, we can insert or delete content at any location as needed
//添加一个分段(在指定下标下插入,并设置动画效果)[segment insertSegmentWithTitle:@"五金电料" atIndex:2 animated:NO];//插入图片分段//[segment insertSegmentWithImage:[UIImage imageNamed:@"需要插入图片的名字"] atIndex:2 animated:YES];//移除一个分段(根据下标) //[segme removeSegmentAtIndex:0 animated:YES];
Insert Title Effect
We can also set other properties
//根据下标修改分段标题(修改下标为2的分段) [segme setTitle:@"巧克力" forSegmentAtIndex:2];//根据内容定分段宽度 segme.apportionsSegmentWidthsByContent = YES; //开始时默认选中下标(第一个下标默认是0) segme.selectedSegmentIndex = 2;//控件渲染色(也就是外观字体颜色) segment.tintColor = [UIColor redColor];//按下是否会自动释放: //segment.momentary = YES;
The effect of the implementation
In addition to the above attributes, we can also set properties for each segment
// 设置指定索引选项的宽度(设置下标为2的分段宽度) [segment setWidth:70.0 forSegmentAtIndex:2]; // 设置分段中标题的位置(0,0点为中心) [segment setContentOffset:CGSizeMake(10,10) forSegmentAtIndex:3];
The modified effect
And most importantly, we're going to add an event to segment.
//添加事件 [segme addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
Implementing the added Event
Clicking on different segments will have different events to correspond to-(void) Change: (Uisegmentedcontrol *) sender{nslog (@ "test"); if (Sender.selectedsegmentindex = 0) { nslog (@ "1");} else if (Sender.selectedsegmentindex = = 1) {nslog (@ "2");} else if (Sender.selectedsegmentindex = = 2) {nslog (@ "3");} else if (Sender.selectedsegmentindex = = 3) {nslog (@ "4");
If there is a need in development, we can make it appear in the navigation bar, we just need to change the method of adding to the view slightly.
//显示在导航栏上self.navigationItem.titleView = segmentedControl;
Effects in the navigation bar
This is just my personal summary of some of the simple use of uisegmentedcontrol, if there is any problem, but also hope that everyone actively pointed out the common progress, thank you.
Text/Snoring ZR (Jane book author)
Original link: Http://www.jianshu.com/p/7d9e4d4368c8
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Method of initialization
//一次性初始化多个control,items可以是图片或者是文字array,系统会自动控制每个单元的大小 //也可通过正常View的初始化进行 - (instancetype)initWithItems:(NSArray *)items;
Event Additions
//通过UIControl的方法来设置,当改变segments的时候通过事件UIControlEventValueChanged, //通过action来处理事件 [self.mySegmentedControl addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventValueChanged];
Content settings for segmented controls
Index settings and get title and image-(void) settitle :(NSString *) title forsegmentatindex:(Nsuinteger) segment; -(NSString *) titleforsegmentatindex:(Nsuinteger) segment; -(void) setimage :(UIImage *) image forsegmentatindex:(Nsuinteger) segment; -(UIImage *) imageforsegmentatindex:(Nsuinteger) segment;
Segmented Controls segments settings
Inserts a new caption at the index point segment-(void) Insertsegmentwithtitle: (NSString *) title Atindex: (Nsuinteger) segment Animated: (BOOL) animated;//inserts a new picture at the index point segment-(void) Insertsegmentwithimage: ( Span class= "hljs-built_in" >uiimage *) image atindex: (nsuinteger) segment animated: (bool) animated; -(void) Removesegmentatindex: (nsuinteger) segment animated :(bool) animated; -(void) Removeallsegmens; //segment number @property ( Nonatomic,readonly) nsuinteger numberOfSegments; //the selected index @property (nonatomic) nsinteger selectedsegmentindex;
Segmented controls display and behavior settings
The default value is no, which remains selected when clicked, and if set to Yes, it is only a short display of the selected state@property (Nonatomic,Getter=ismomentary)BOOL momentary;The width of each segment is adjusted by index, and if set to 0.0, it is automatically resized and defaults to 0.0-(void) SetWidth: (CGFloat) Width Forsegmentatindex: (Nsuinteger) segment; - (CGFloat) Widthforsegmentatindex: (Nsuinteger) segment;Adjust the text or image by index offset in segment, default to (0,0)-(void) Setcontentoffset: (cgsize) offset Forsegmentatindex: (nsuinteger) segment; -(cgsize) Contentoffsetforsegmentatindex: (nsuinteger) segment; //Set whether segment can be selected-(void) setenabled: (BOOL) enabled Forsegmentatindex: (nsuinteger) segment;-( BOOL) Isenabledforsegmentatindex: (nsuinteger) segment; //Whether the width is automatically adjusted according to the size of the content @property (nonatomic) BOOL apportionssegmentwidthsbycontent ns_available _ios (5_0);
Customizing the Appearance
Set Uisegmentedcontrol border, divider line, text, click Color@property (Nonatomic,RetainUicolor *tintcolor;Get and set the background picture, through Uicontrolstate and Barmetrics to set up and get, main need first setUIControlStateNormal will be effective-(UIImage *) Backgroundimageforstate: (uicontrolstate) State barmetrics: (Uibarmetrics) Barmetrics-(void) SetBackgroundImage: (UIImage *) backgroundimage forstate: (uicontrolstate) State barmetrics: (Uibarmetrics) BarmetricsTo set the picture style of a split line, you first need to set the UIControlStateNormal whether left or right,After the setting is complete, the setting method is called and the split line is displayed according to Uicontrolstate-(void) Setdividerimage: (UIImage *) Dividerimage forleftsegmentstate: (uicontrolstate) leftstate rightsegmentstate: (uicontrolstate) rightstate Barmetrics: (Uibarmetrics) Barmetrics-(UIImage *) Dividerimageforleftsegmentstate: (uicontrolstate) leftstate rightsegmentstate: (uicontrolstate) rightstate Barmetrics: (Uibarmetrics) BarmetricSets the style of the title, including font, text color, shadow. Through the key-value pairs inside the NSAttributedString.h-(void) Settitletextattributes: (Nsdictionary *) attributes Forstate: (Uicontrolstate) stateNs_available_ios (5_0)Ui_appearance_selector; - (Nsdictionary *) Titletextattributesforstate: (Uicontrolstate) stateNs_available_ios (5_0)ui_appearance_selector; //adjust the position of title and image by a given segment-(void) Setcontentpositionadjustment: (uioffset) adjustment forsegmenttype: (uisegmentedcontrolsegment) leftcenterrightoralone barmetrics: ( Uibarmetrics) barmetrics ns_available_ios (5_0) ui_appearance_selector; -(uioffset) Contentpositionadjustmentforsegmenttype: ( uisegmentedcontrolsegment) Leftcenterrightoralone barmetrics: (UIBarMetrics) Barmetrics ns_available_ios (5_0) ui_appearance_selector;
iOS Development Segment Controller (UISEGMENTEDCONTROL)