系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需

这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控件的宽度平均分配每一项的宽度,如果设置了,那么总宽度超过控件宽度后会有滑动效果

直接上代码吧:

头文件:

#import <Foundation/Foundation.h>

@protocol WCSegmentControlDelegate

-(void)wcSegmentControlSelectionChanged:(id)sender;

@end

@interface WCSegmentControl : UIView

@property (nonatomic, strong)id<WCSegmentControlDelegate>delegate;

@property (nonatomic, strong) NSMutableArray *dataSourceOFTitle;
@property (nonatomic, assign, setter=setCurrentSelectedIndex:) int currentSelectedIndex; //title color
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, strong) UIColor *selectedTitleColor; //font
@property (nonatomic, strong) UIFont *titleFont; //item selectedBackground Color;
@property (nonatomic, strong) UIColor *itemBackgroundColor;
@property (nonatomic, strong) UIColor *selectedItemBackgroundColor; //item selectedBackground image;
@property (nonatomic, strong) UIImage *itemBackgroundImage;
@property (nonatomic, strong) UIImage *selectedItemBackgroundImage; //item border
@property (nonatomic, strong) UIColor *itemBorderColor;
@property (nonatomic, assign) BOOL isShowItemBorderWhenHilight;
@property (nonatomic, assign) int itemBorderWidth;
@property (nonatomic, assign) int itemCornerRadius; //item, 不设置则均分控件宽度
@property (nonatomic, assign) int itemWidth; //control border
@property (nonatomic, strong) UIColor *borderColor;
@property (nonatomic, assign) BOOL isShowBorder;
@property (nonatomic, assign) int borderWidth;
@property (nonatomic, assign) int cornerRadius; //分割线
@property (nonatomic, strong) UIColor *splitColor;
@property (nonatomic, assign) int splitBorderWidth;
@property (nonatomic, assign) BOOL isShowSplitBorder; @end

实现文件:

#import "WCSegmentControl.h"
#import "WCSegmentControlItemButton.h" @implementation WCSegmentControl {
UIScrollView *_scrollView;
} - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.clipsToBounds = YES; _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.backgroundColor = self.backgroundColor;
[self addSubview:_scrollView]; _dataSourceOFTitle = [NSMutableArray array];
_selectedTitleColor = [UIColor whiteColor];
_titleColor = [UIColor blackColor];
_itemBackgroundColor = [UIColor whiteColor];
_selectedItemBackgroundColor = kWCColor7; _borderWidth = ;
_borderColor = kWCColor7;
_cornerRadius = ; _itemBorderColor = kWCColor7;
_itemBorderWidth = ;
_itemCornerRadius = ; _titleFont = [UIFont systemFontOfSize:]; _splitColor = kWCColor7;
_splitBorderWidth = ; _isShowBorder = YES;
_isShowItemBorderWhenHilight = NO;
_isShowSplitBorder = YES;
} return self;
} - (void)setDataSourceOFTitle:(NSMutableArray *)dataSourceOFTitle {
_dataSourceOFTitle = dataSourceOFTitle; [self reloadData];
} -(WCSegmentControlItemButton *)createItemControlWithTitle:(NSString *)title {
WCSegmentControlItemButton * btn = [[WCSegmentControlItemButton alloc] init]; if(_itemBackgroundImage)
{
[btn setBackgroundImage:_itemBackgroundImage forState:UIControlStateNormal];
}
if(_selectedItemBackgroundImage)
{
[btn setBackgroundImage:_selectedItemBackgroundImage forState:UIControlStateSelected];
}
[btn setBackgroundColor:_itemBackgroundColor];
[btn setTitleColor:_selectedTitleColor forState:UIControlStateSelected];
[btn setTitleColor:_titleColor forState:UIControlStateNormal];
btn.titleLabel.font = _titleFont;
[btn setTitle:title forState:UIControlStateNormal];
btn.layer.cornerRadius = _itemCornerRadius; return btn;
} - (void)refreshUI {
if (_isShowBorder) {
self.layer.borderWidth = _borderWidth;
self.layer.borderColor = _borderColor.CGColor;
self.layer.cornerRadius = _cornerRadius;
} else {
self.layer.borderWidth = ; }
}
-(void) reloadData
{ [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; if ([_dataSourceOFTitle count] > ) { // UIEdgeInsets
CGRect fra = CGRectMake(
,
,
_itemWidth > ? _itemWidth : self.width / [_dataSourceOFTitle count],
self.height); CGFloat leftMargin = MAX(, (self.width -fra.size.width* [self.dataSourceOFTitle count])/ );
__block CGFloat contentWidth = leftMargin;
[self.dataSourceOFTitle enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {
WCSegmentControlItemButton *btn = [self createItemControlWithTitle:title];
btn.frame = fra;
btn.left =leftMargin + idx * btn.width;
[btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
btn.index = idx;
[_scrollView addSubview:btn]; if (_isShowSplitBorder && idx != ) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(, , _splitBorderWidth, btn.height)];
line.backgroundColor = _splitColor;
line.left = btn.left;
[_scrollView addSubview:line];
}
contentWidth = btn.right;
}]; _scrollView.contentSize = CGSizeMake(contentWidth, _scrollView.height); [self setCurrentSelectedIndex:];
[self refreshUI]; }
} -(void) btnTapped:(id) sender
{
WCSegmentControlItemButton *btn = sender;
if (self.currentSelectedIndex == btn.index) {
return;
} [self setCurrentSelectedIndex:btn.index]; //不可以在setCurrentSelectedIndex触发,否则会造成重复执行
if ([(NSObject *) (self.delegate) respondsToSelector:@selector(wcSegmentControlSelectionChanged:)]) {
[self.delegate wcSegmentControlSelectionChanged:self];
}
} -(void) setCurrentSelectedIndex:(int) currentSelectedIndex
{
_currentSelectedIndex = currentSelectedIndex; [_scrollView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[WCSegmentControlItemButton class]]) {
WCSegmentControlItemButton *view = obj;
if (view.index == currentSelectedIndex) {
[view setSelected:YES];
[view setBackgroundColor:_selectedItemBackgroundColor]; //如果在屏幕外则需要移动到屏幕中
if (view.right - _scrollView.width > ) {
_scrollView.contentOffset = CGPointMake(view.right - _scrollView.width, )
;
}else if (view.left - _scrollView.contentOffset.x < ) {
_scrollView.contentOffset = CGPointMake(view.left, );
} if (_isShowItemBorderWhenHilight) {
view.layer.borderWidth = _borderWidth;
view.layer.borderColor = _borderColor.CGColor;
view.layer.cornerRadius = _cornerRadius;
} } else {
[view setSelected:NO];
[view setBackgroundColor:_itemBackgroundColor]; view.layer.borderWidth = ; } } }];
} @end

IOS开发--自定义segment控件,方便自定义样式的更多相关文章

  1. iOS 开发 ZFUI framework控件,使布局更简单

    来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...

  2. ios开发中button控件的属性及常见问题

    最为最基本的控件,我们必须对button的每个常用属性都熟练应用: 1,使用之前,必须对按钮进行定义,为乐规范,在@interface ViewController (){}中进行定义,先定义后使用. ...

  3. IOS开发中设置控件内容对齐方式时容易混淆的几个属性

    IOS开发中四个容易混淆的属性: 1. textAligment : 文字的水平方向的对齐方式 1> 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NST ...

  4. iOS开发基础-UITableView控件简单介绍

     UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动.  UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么 ...

  5. iOS开发无第三方控件的援助达到的效果侧边栏

    最近的研究iOS程序侧边栏.渐渐的发现iOS该方案还开始采取风侧边栏格该,QQ,今日头条,Path(Path运营商最早的侧边栏app该,效果说成是Path效果),所以就研究了下. 然后发现Git Hu ...

  6. iOS开发中UIDatePicker控件的使用方法简介

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四 ...

  7. IOS开发之按钮控件Button详解

    reference:http://mxcvns.lofter.com/post/1d23b1a3_685d59d 首先是继承问题,UIButton继承于UIControl,而UIControl继承于U ...

  8. ios开发之--系统控件显示中文

    虽然一直知道X-code肯定提供有语言本地化的设置地方,但是一直也做个记录,有些时候的汉化,还是需要使用代码去控制,键盘的右下角.navagiton的return使用代码修改,调用系统相机时,也是出现 ...

  9. Android View体系(十)自定义组合控件

    相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...

随机推荐

  1. Excel 改变列表头显示方式, Excel显示列数字

    '显示数字列号 Sub showCellNumber() Application.ReferenceStyle = xlR1C1 End Sub '显示字母列号 Sub showCellZimu() ...

  2. js地区转盘抽奖插件

    <script type="text/javascript"> var colors = ["#B8D430", "#3AB745&quo ...

  3. Win10/UWP开发—SystemNavigationManager

    Win10系统为确保所有应用中的一致导航体验,提供后退导航功能.当你的应用在手机.平板电脑上或者在支持系统后退功能的电脑或笔记本电脑上运行时,系统会在"后退"按钮被按下时通知你的应 ...

  4. UWP/Win10新特性系列—Drag&Drop 拖动打开文件

    在Win10 App开发中,微软新增了系统PC文件与UWP 之间的文件拖拽行为,它支持将系统磁盘上的文件以拖拽的形式拖入App中并处理,在前不久的微软build 2015开发者大会上微软展示的UWP版 ...

  5. 【转】浅思OC的语言特性

    算了算,学习IOS已经有一段时间了.今天花了点时间思考一下OC的语言特性,让自己的心不要那么浮躁,注重基础,回归本源. OC做为一门面向对象语言,自然具有面向对象的语言特性,如封装.继承.多态.他具有 ...

  6. C语言中关于对目录的操作

    原文地址:C语言中关于对目录的操作 目录的操作不论是在嵌入式产品还是应用软件编程都是必不可少的,不同的开发语言可能略有不同,笔者主要是讨论在Linux平台下对目录的一系列操作: 1.获取当前目录操作: ...

  7. IOS7.0 UILabel实现自适应高度的新方法

    //IOS7.0中利用- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attri ...

  8. 遗传算法,实数编码的交叉操作之SBX(模拟二进制交叉)

    本文主要介绍遗传算法(实数编码)的交叉操作中的SBX,模拟二进制交叉. 首先,给出个人用python2.7实现的代码,具体模块已上传到: https://github.com/guojun007/sb ...

  9. Mysql使用mysqldump按时间导出时的一个注意事项

    在使用Mysql的mysqldump命令按时间进行数据导出时,我们可能会遇到导出数据与原库中不符或者导出数据和我们期望的数据不同的情况. 我们知道Mysql数据库导出可以使用命令mysqldump,使 ...

  10. The Non-Inverting Amplifier Output Resistance by Adrian S. Nastase [ Copied ]

    Source Address: http://masteringelectronicsdesign.com/the-non-inverting-amplifier-output-resistance/ ...