- UISegmentedControl
创建:
UISegmentedControl *segmentedC = [[UISegmentedControl alloc] initWithItems:@[@"1safgfga", @"2", @"3"]];
-
常用属性和方法
1.分段标签的宽度随内容自适应(默认 NO:所有分段宽度一样,取最大分段宽度)
segmentedC.apportionsSegmentWidthsByContent = YES;
2.调整分段的宽度
[segmentedC setWidth:100 forSegmentAtIndex:1];
3.重设分段内容
[segmentedC setTitle:@"xd" forSegmentAtIndex:1];
4.重设分段图片
[segmentedC setImage:[UIImage imageNamed:@""] forSegmentAtIndex:3];
5.设置分段风格颜色
segmentedC.tintColor = [UIColor redColor];
6.背景图片
[segmentedC setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
typedef NS_ENUM(NSInteger, UIBarMetrics) {
UIBarMetricsDefault,
UIBarMetricsCompact,
UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
UIBarMetricsCompactPrompt,
UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact,
UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt,
};
7.设置分段分割线
[segmentedC setDividerImage:[UIImage imageNamed:@""] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
8.设置分段内文字属性
[segmentedC setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
9.添加一个文字新分段
[segmentedC insertSegmentWithTitle:@"gg" atIndex:0 animated:YES];
10.添加一个图片新分段
[segmentedC insertSegmentWithImage:[UIImage imageNamed:@""] atIndex:2 animated:YES];
11.移除所有分段
[segmentedC removeAllSegments];
12.移除对应的分段
[segmentedC removeSegmentAtIndex:0 animated:YES];
13.设置分段内容偏移量
[segmentedC setContentOffset:CGSizeMake(20, 40) forSegmentAtIndex:1];
14.设置分段内容偏移量
[segmentedC setContentPositionAdjustment:UIOffsetMake(100, 100) forSegmentType:UISegmentedControlSegmentLeft barMetrics:UIBarMetricsDefault];
typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {
UISegmentedControlSegmentAny = 0, // 所有分段受影响
UISegmentedControlSegmentLeft = 1, // The capped, leftmost segment. Only applies when numSegments > 1.
UISegmentedControlSegmentCenter = 2, // Any segment between the left and rightmost segments. Only applies when numSegments > 2.
UISegmentedControlSegmentRight = 3, // The capped,rightmost segment. Only applies when numSegments > 1.
UISegmentedControlSegmentAlone = 4, // The standalone segment, capped on both ends. Only applies when numSegments = 1.
};
15.是否保持选中状态(默认NO:不保持选中状态)
segmentedC.momentary = YES;
16.禁用
[segmentedC setEnabled:NO forSegmentAtIndex:0];
-
获取信息的方法
NSString *title = [segmentedC titleForSegmentAtIndex:1];
UIImage *image = [segmentedC imageForSegmentAtIndex:3];
CGFloat width = [segmentedC widthForSegmentAtIndex:0];
CGSize size = [segmentedC contentOffsetForSegmentAtIndex:1];
BOOL isEnable = [segmentedC isEnabledForSegmentAtIndex:0];
UIImage *bgImage = [segmentedC backgroundImageForState:UIControlStateNormal barMetrics:UIBarMetricsCompact];
UIImage *dividerImage = [segmentedC dividerImageForLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
NSDictionary *titleAttir = [segmentedC titleTextAttributesForState:UIControlStateNormal];
NSInteger selectedIndex = segmentedC.selectedSegmentIndex;
NSInteger count = segmentedC.numberOfSegments;
UIOffset offset = [segmentedC contentPositionAdjustmentForSegmentType:UISegmentedControlSegmentLeft barMetrics:UIBarMetricsDefault];