系统的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. PKu 2195

    //PKu 2195 回家 By Loli_con Enail : Loli_con@outlook.com /* 题目叙述 ========= 在一个网格图中,有n个人和n个房子.每一个单位时间,每 ...

  2. windows核心编程---第九章 同步设备IO与异步设备IO之同步IO

    同步设备IO 所谓同步IO是指线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备的通知.而IO完成端口就是实现这种通知的很 ...

  3. 初识 Burp Suite

           Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程. 所有的工具都共享一个能处理并显示HTTP 消息, ...

  4. 用Unity代码通过Xml配置生成GameObject之——前两天掉的坑

    1. Resources.Load(path),path不是绝对路径,而是相对"Resources/"的相对路径!如: 要想Instantiate则代码应该如下: string m ...

  5. React Native工作小技巧及填坑记录

    以下是本人在React Native开发工作中使用的一些小技巧,记录一下. 1.从网络上拉取下来的React Native缺少React和React Native库. 终端 1. cd 项目根目录 2 ...

  6. Qt for Android开发Android应用时的各种错误汇总(此片博文不成熟,请大家略过)

    “Qt for Android真的很脆弱,项目能跑起来靠的是奇迹,跑不起来,各种报错才是正常...” 问题一:Qt for Android编译不过:make (e=2): 系统找不到指定的文件. 之前 ...

  7. DeepLearning入门笔记(一),准备工作与注意事项

    本文记录了安装theano.keras.tensorflow以及运行tutorial程序时遇到的一些问题,供后人参考. 实验机器:联想笔记本,i7-6700HQ,GTX960M,16G内存,SSD硬盘 ...

  8. Apache commons-client authentication(授权)

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswo ...

  9. check_env函数解析

    又是一个比较长的函数,是用来检查文件权限,目录条件的.具体代码如下: check_env() { # Check user privilege. #检查用户权限 check_user root # C ...

  10. overflow:hidden清楚浮动的影响

    在网页布局中有时会遇到这种情况: 如果左边用<dt>,右边用<dd>,放在一行显示,<dt>要设置float:left,这个应该都知道,问题是,第一行这样做没有问题 ...