IOS开发--自定义segment控件,方便自定义样式
系统的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控件,方便自定义样式的更多相关文章
- iOS 开发 ZFUI framework控件,使布局更简单
来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...
- ios开发中button控件的属性及常见问题
最为最基本的控件,我们必须对button的每个常用属性都熟练应用: 1,使用之前,必须对按钮进行定义,为乐规范,在@interface ViewController (){}中进行定义,先定义后使用. ...
- IOS开发中设置控件内容对齐方式时容易混淆的几个属性
IOS开发中四个容易混淆的属性: 1. textAligment : 文字的水平方向的对齐方式 1> 取值 NSTextAlignmentLeft = 0, // 左对齐 NST ...
- iOS开发基础-UITableView控件简单介绍
UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动. UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么 ...
- iOS开发无第三方控件的援助达到的效果侧边栏
最近的研究iOS程序侧边栏.渐渐的发现iOS该方案还开始采取风侧边栏格该,QQ,今日头条,Path(Path运营商最早的侧边栏app该,效果说成是Path效果),所以就研究了下. 然后发现Git Hu ...
- iOS开发中UIDatePicker控件的使用方法简介
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Timer四 ...
- IOS开发之按钮控件Button详解
reference:http://mxcvns.lofter.com/post/1d23b1a3_685d59d 首先是继承问题,UIButton继承于UIControl,而UIControl继承于U ...
- ios开发之--系统控件显示中文
虽然一直知道X-code肯定提供有语言本地化的设置地方,但是一直也做个记录,有些时候的汉化,还是需要使用代码去控制,键盘的右下角.navagiton的return使用代码修改,调用系统相机时,也是出现 ...
- Android View体系(十)自定义组合控件
相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...
随机推荐
- 为PHP安装两个扩展,memcache和Redis
最近由于学习Discuz,发现Discuz对于memcache和Redis都有支持,为了看一看开启这些支持之后的神器效果,所以在window上配置了下这两个扩展 环境说明:OS:windows 7 服 ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Excel 改变列表头显示方式, Excel显示列数字
'显示数字列号 Sub showCellNumber() Application.ReferenceStyle = xlR1C1 End Sub '显示字母列号 Sub showCellZimu() ...
- nginx 在windows平台上对asp.net做反向代理
代理服务器 当客户机向站点提出请求时,请求将转到代理服务器.然后,代理服务器通过防火墙中的特定通路,将客户机的请求发送到内容服务器.内容服务器再通过该通道将结果回传给代理服务器.代理服务器将检索到的信 ...
- JAVAWEB学习总结 HttpServletResponse对象(一)
Web服务器收到客户端(浏览器)的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象. request和response对象既然代表请求和响 ...
- C++深拷贝与浅拷贝
当用一个已初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用.也就是说,当类的对象需要拷贝时,拷贝构造函数将会被调用.以下情况都会调用拷贝构造函数: (1)一个对 ...
- Shiro安全登录框架
环境准备 本文使用Maven构建,因此需要一点Maven知识.首先准备环境依赖: <dependencies> <dependency> <groupId>juni ...
- Android Handler 最佳的理解资料
- Ansible-Tower快速入门-4.以超级用户帐号登录【翻译】
以超级用户帐号登录 首先,登录tower需要使用tower服务器所在的URL,格式如下:https://<tower server name>/ 注意:tower安装了一个自签名证书用于H ...
- java 静态代理-积木系列
代理模式的定义:Provide a surrogate or placeholder for another object to controlaccess to it(为其他对象提供一种代理以控制对 ...