为 iOS 定制的分段控件。
NSArray *titles = @[@"Left", @"Medium", @"Right"];
YUSegment *segment = [[YUSegment alloc] initWithTitles:titles];
[self.view addSubview:segment];
segment.frame = (CGRect){20, 60, [UIScreen mainScreen].bounds.size.width - 40, 44};
更改视图的位置和大小。
在标识检查器中,将类更改为 "YUSegment"。然后连接出口。
@property (weak, nonatomic) IBOutlet YUSegment *segment;
可以在属性检查器中修改一些属性。
更多详情请查看 YUSegmentDemo。
与 UISegmentedControl 类似,您只需要以下代码
[segment addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventValueChanged];
默认情况下,图片相同,无论选中哪个。如果您想在特定段选中时显示不同的图片,应调用 -replaceDeselectedImagesWithImages:
或 -replaceDeselectedImageWithImage:atIndex:
。
请使用 -setTitleTextAttributes:forState:
来设置属性字符串。例如
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:20]};
[segment setTitleTextAttributes:attributes forState:YUSegmentedControlStateNormal];
您可以在属性检查器中设置 borderColor
、borderWidth
和 cornerRadius
。如果您不使用界面构建器,代码应类似于以下:
segment.layer.borderWidth = someValue;
segment.layer.borderColor = someValue;
注意:您应使用 注意:关于 cornerRadius
而不是 layer.cornerRadius
。因为如果您为分段控件设置了 cornerRaduis
,则指示器将自动变为圆形。cornerRadius
。分段控件及其指示器都有默认的 cornerRadius
值。默认值等于分段控件高度的一半。在版本 0.2.0 中,如果不设置分段控件的 cornerRaduis
,指示器不会自动变为圆形。您还需要手动为指示器设置 cornerRadius
。
YUSegment 没有类似于 scrollEnabled
的属性,您只需要设置每个分段的宽度(segmentWidth
)。此属性将使分段控件水平滚动。
borderWidth
和 borderColor
,使其看起来像:sgement.borderWidth(1.0).borderColor([UIColor redColor]);
indicator(只读)
,您可以为指示器设置属性,例如 backgroundColor
、borderWidth
、borderColor
等。segment.indicator.backgroundColor = [UIColor redColor];
segment.indicator.layer.borderWidth = 1.0;
segment.indicator.layer.borderColor = [UIColor redColor].CGColor;
borderWidth
和 borderColor
,使其看起来像:segment.indicator.borderWidth(1.0).borderColor([UIColor redColor]);
该代码根据 MIT 许可协议的条款和条件分发。