XWSegmentedControl 1.0.1

XWSegmentedControl 1.0.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2017年11月

小文撸码维护。



  • 作者:
  • 小文撸码

简介

XWSegmentControl是一个用于页面切换的分段控件,采用object-c编写,支持手动加载和xib加载,颜色可自定义、内置样式;简单易用...

引用

  1. 可以将项目中的XWSegmentControl.h和XWSegmentControl.m文件拖入项目
  2. 也可以使用cocoapods输入命令 pod 'XWSegmentedControl'

效果

使用效果
使用效果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];