XWSegmentControl是一个用于页面切换的分段控件,采用object-c编写,支持手动加载和xib加载,颜色可自定义、内置样式;简单易用...
初始化
NSMutableArray *views =[[NSMutableArray alloc] init];
NSArray *titles = @[@"测试1",@"测试2",@"测试3",@"测试4"];
for (int i = 0; i<4; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.mySegmentedCtrl.frame), [[UIScreen mainScreen] bounds].size.width, 300)];
UILabel*label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 100, 21)];
label.text = [titles objectAtIndex:i];
[view addSubview:label];
[self.view addSubview:view];
[views addObject:view];
}
self.mySegmentCtrl = [[XWSegmentedControl alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 40)];
[self.mySegmentedCtrl setSegmentedControlTitles:titles andViews:views];
self.mySegmentedCtrl.delegate = self;
设置是否有标题下划线、中间分割线
mySegmentCtrl.isHasTitleBottomLine = NO;
mySegmentCtrl.isHasTitleCenterLine = YES;
设置线条样式
mySegmentCtrl.lineStyle = XWSegmentedControlLineStyleEqualTitle;
设置字体和颜色
NSDictionary *selectedTextAttributes = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:17],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
NSDictionary *unselectedTextAttributes = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:17],
NSForegroundColorAttributeName : [UIColor redColor]
};
[_mySegmentCtrl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
[_mySegmentCtrl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
设置选中和未选中颜色
_mySegmentCtrl.normalLineColor = [UIColor blueColor];
_mySegmentCtrl.selectLineColor = [UIColor redColor];
设置背景色
_mySegmentCtrl.backgroundColor = [UIColor greenColor];