在iOS中有一个控件,叫UISegmentedControl,有的时候,系统的控件不能满足我们的要求的时候,我们需要仿照系统的控件,做一个自己的属于自己的个性化的控件,如图所示:
步骤:
一:结构是,一个view中有4个按钮,左侧按钮和右侧按钮分别有不同的背景图片,因为左右两边带有圆角效果,中间的按钮也是一种背景图片,不需要圆角效果。
如图:
二:粘贴代码:
------------------------------------------------------------------------------------------------------------------------------------------------------------
.h文件:
#import <UIKit/UIKit.h>
@class CZSegmentedControl;
@protocol CZSegmentedControlDelegate <NSObject>
@optional
/**
* 按钮被选择
*/
-(void)segmentedControl:(CZSegmentedControl *)segm didSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex;
@end
@interface CZSegmentedControl : UIView
// 代理
@property(nonatomic,weak) id<CZSegmentedControlDelegate> delegate;
//选中某一个按钮的索引
@property(nonatomic,assign) NSInteger selectedSegmentIndex;
//初始化
-(instancetype)initWithItems:(NSArray *)items;
@end
------------------------------------------------------------------------------------------------------------------------------------------------------------.m文件:
#import "CZSegmentedControl.h"
@interface CZSegmentedControl ()
/**
* 被选中的按钮
*/
@property(nonatomic,strong) UIButton *selectedBtn;
@end
@implementation CZSegmentedControl
-(void)addBtnWithNormalBgImgName:(NSString *)normalBgImgName selectedBgImgName:(NSString *)selectedBgImgName title:(NSString *)title{
//初始化按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 设置标题
[btn setTitle:title forState:UIControlStateNormal];
//字体大小
btn.titleLabel.font = [UIFont systemFontOfSize:14];
//字体颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置正常背景图片和选中的背景图片
[btn setResizedNormalBg:normalBgImgName];
[btn setResizedSelectedBg:selectedBgImgName];
//设置按钮的tag
btn.tag = self.subviews.count;
//添加监听
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//默认第一个按钮被选中
if(self.subviews.count == 0){
btn.selected = YES;
self.selectedBtn = btn;
}
[self addSubview:btn];
}
/**
* 按钮的点击事件
*/
-(void)btnClick:(UIButton *)btn{
//通知代理
if([self.delegate respondsToSelector:@selector(segmentedControl:didSelectedFromIndex:toIndex:)]){
[self.delegate segmentedControl:self didSelectedFromIndex:self.selectedBtn.tag toIndex:btn.tag];
}
//设置先前按钮的选中状态
self.selectedBtn.selected = NO;
//设置当前按钮的选中状态
btn.selected = YES;
//将当前按钮赋值给选中按钮
self.selectedBtn = btn;
}
/**
* 重新布局
*/
-(void)layoutSubviews{
[super layoutSubviews];
NSLog(@"%s",__func__);
//子控件的个数
int count = self.subviews.count;
//按钮的宽度
CGFloat btnW = self.frame.size.width / count;
//按钮的高度
CGFloat btnH = self.frame.size.height;
CGFloat btnY = 0;
for (int i=0; i<count; i++) {
UIButton *btn = (UIButton *)self.subviews[i];
CGFloat btnX = i * btnW;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
-(instancetype)initWithItems:(NSArray *)items{
if(self = [super init]){
//添加按钮
for (int i =0 ; i<items.count; i++) {
NSString *title = (NSString *)items[i];
if(i == 0){ //第一个
[self addBtnWithNormalBgImgName:@"SegmentedControl_Left_Normal" selectedBgImgName:@"SegmentedControl_Left_Selected" title:title];
}else if(i == items.count -1){ //最后一个
[self addBtnWithNormalBgImgName:@"SegmentedControl_Right_Normal" selectedBgImgName:@"SegmentedControl_Right_Selected" title:title];
}else{ // 中间
[self addBtnWithNormalBgImgName:@"SegmentedControl_Normal" selectedBgImgName:@"SegmentedControl_Selected" title:title];
}
}
}
return self;
}
/**
* 设置按钮被选中
*
* @param selectedSegmentIndex 选中按钮的索引
*/
-(void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex{
if(selectedSegmentIndex >=0 && selectedSegmentIndex <= (self.subviews.count - 1)){
_selectedSegmentIndex = selectedSegmentIndex;
self.selectedBtn.selected = NO;
UIButton *btn = (UIButton *)self.subviews[selectedSegmentIndex];
btn.selected = YES;
self.selectedBtn = btn;
}
}
注:使用代码如下:
//初始化segmentedControl
CZSegmentedControl *segm = [[CZSegmentedControl alloc] initWithItems:items];
//设置frame
segm.frame = CGRectMake(0, 0, 300, 32);
//设置代理
segm.delegate = self;
//设置titleView
self.navigationItem.titleView = segm;
------------------------------------------------------------------------------------------------------------------------------------------------------------
//代理方法
-(void)segmentedControl:(CZSegmentedControl *)segm didSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
NSLog(@"from : %d to : %d",fromIndex,toIndex);
}