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体系(四)从源 ...
随机推荐
- TE9手机微信场景
HTML <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...
- Java里面,反射父类里面数字类型字段,怎么set值
Java里面,反射父类里面数字类型字段,怎么set值,我的做法是这样: /** * TODO 直接设置对象属性值, 忽略private/protected 修饰符, 也不经过setter * @aut ...
- 领域设计之模型充血、Repository对象注入
工作中接触了不少项目组,他们在实际的项目开发中,Domain Object的贫血模型设计,还是主要的应用的范式.原因在于,贫血模型模型设计中,把所有涉及持久化的业务逻辑,封装到了Domain Serv ...
- Android TextView设置多彩文字
在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求; 以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心; 直到接触到了SpannableStringBuilde ...
- Mac下用brew安装nginx
1. nginx nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TC ...
- KI的斐波那契_DFS
Description KI十分喜欢美丽而优雅的斐波那契数列,最近他新认识了一种斐波那契字符串,定义如下 f (0) = b, f (1) = a, f (2) = f (1) + f (0) = a ...
- Unity3D教程:茄子童萌會
http://s.epb.idv.tw/han-shi-ku/unity Unity 0000 Unity3D學習之路 - C#學習筆記(一) 0001 Unity3D學習之路 - C#學習筆記(二) ...
- CTO干点啥?
1.负责技术 2.负责人才 3.负责业务(需求) 4.负责组织
- C#、js、json Datetime格式总结
在工作过程中遇到时间格式的数据在C#.js 和 json保存的不同结果,现在总结一下 JavaScript Parser: 1.数字型时间转字符串时间 如var data = "/Date( ...
- RBM阅读笔记
RBM包含两个层,可见层(visble layer)和隐藏层(hidden layer).神经元之间的连接具有以下特点:层内无连接,层间全连接.RBM可以看做是一个二分图(神经元当做顶点,神经元之间的 ...