系统的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 = 1; _borderColor = kWCColor7; _cornerRadius = 5; _itemBorderColor = kWCColor7; _itemBorderWidth = 1; _itemCornerRadius = 0; _titleFont = [UIFont systemFontOfSize:12]; _splitColor = kWCColor7; _splitBorderWidth = 1; _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 = 0; } } -(void) reloadData { [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; if ([_dataSourceOFTitle count] > 0) { // UIEdgeInsets CGRect fra = CGRectMake( 0, 0, _itemWidth > 0 ? _itemWidth : self.width / [_dataSourceOFTitle count], self.height); CGFloat leftMargin = MAX(0, (self.width -fra.size.width* [self.dataSourceOFTitle count])/2 ); __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 != 0) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _splitBorderWidth, btn.height)]; line.backgroundColor = _splitColor; line.left = btn.left; [_scrollView addSubview:line]; } contentWidth = btn.right; }]; _scrollView.contentSize = CGSizeMake(contentWidth, _scrollView.height); [self setCurrentSelectedIndex:0]; [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 > 0) { _scrollView.contentOffset = CGPointMake(view.right - _scrollView.width, 0) ; }else if (view.left - _scrollView.contentOffset.x < 0) { _scrollView.contentOffset = CGPointMake(view.left, 0); } 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 = 0; } } }]; } @end