# XWSegmentedControl **Repository Path**: xwlm/XWSegmentedControl ## Basic Information - **Project Name**: XWSegmentedControl - **Description**: 分段控件 - **Primary Language**: Objective-C - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2017-02-17 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 简介 XWSegmentControl是一个用于页面切换的分段控件,采用object-c编写、支持手动加载和xib加载,颜色可自定义、内置样式;简单好用... ### 引用 1. 可以将项目中的XWSegmentControl.h和XWSegmentControl.m 文件拉入项目 2. 也可以用cocoapods 输入命令 pod 'XWSegmentedControl' ### 效果 ![使用效果](http://git.oschina.net/uploads/images/2017/0321/105351_d2a14939_1219353.png "使用效果") ![使用效果2](http://git.oschina.net/uploads/images/2017/0321/103926_3d76da6a_1219353.png "使用效果2") ### 使用代码 初始化 ``` 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]; ```