系统的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. Varnish介绍

    “Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang (http://www.vg.no) 使用3台Varnish代替了原来的12台squid,性能居然比以前 ...

  2. javascript中值传递与值引用的研究

    今天重新看了一下<javascript高级程序设计>,其中讲到了javascript中的值传递和值引用,所以就自己研读了一下,但是刚开始没有明白函数中的参数只有值传递,有的场景好像参数是以 ...

  3. CentOS下解压缩命令

    ——————————————— .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) —————— ...

  4. matlab mesh visualization

    1. matlab color specification http://au.mathworks.com/help/matlab/ref/colorspec.html

  5. mac中使用brew安装软件,下载太慢怎么办?

    mac中使用brew安装软件,下载太慢怎么办? 本文所说的软件是指较大的软件,如果软件较小,例如软件只有几M,那么使用此方法后,提升会非常小. 了解brew原理: 1: 从网络下载安装包 2: 执行一 ...

  6. 修改SharePoint 2013中item Created by 信息

    因为公司的系统有点小bug.额,要做点坏事,把系统没记上的东西偷偷补上去,但是item的created by变成了我(这怎么行,不能让别人知道我做了坏事,一定是隔壁小李干的! 懒得开visual st ...

  7. 类和对象 nil/Nil/NULL的区别

    iOS-----类和对象,nil/Nil/NULL的区别   iOS中类和对象,nil/Nil/NULL的区别 类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性 ...

  8. 2016年中国大学生程序设计竞赛(合肥)-重现赛1008 HDU 5968

    异或密码 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  9. php 获取静态方法调用的类名

    方法一: class foo { static public function test() { var_dump(get_called_class()); } } class bar extends ...

  10. [ JS 进阶 ] 基本类型 引用类型 简单赋值 对象引用

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型,拥有方法的类型和不能拥有方法的类型,还可以分为可变类型和不可变类型,其实这些叫法都是依据这两 ...