自己封装一个View
//在.h声明
@interface SelectView : UIView
@property(nonatomic,assign)NSInteger selectIndex;
- (instancetype)initWithTitle:(NSString *)title andItemArrar:(NSArray *)items;
@end
//在.m里面实现
#import "SelectView.h"
#import "Masonry.h"
@interface SelectView()
@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UIButton * selectBtn;
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSArray * items;
@end
@implementation SelectView
- (instancetype)initWithTitle:(NSString *)title andItemArrar:(NSArray *)items{
if (! (self = [super init]))
return nil;
self.backgroundColor = [UIColor yellowColor];
_title = title;
_items = items;
[self addSubview:self.titleLabel];
[self layoutPageFrame];
[self addSubview:self.selectBtn];
return self;
}
//点击事件
- (void)selectBtnClick:(UIButton *)sender{
//多选模式
// if (!sender.selected) {
// [sender setBackgroundColor:[UIColor brownColor]];
// }else{
// [sender setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
// }
// sender.selected = !sender.selected;
//单选模式
if (_selectBtn == nil){
sender.selected = YES;
_selectBtn = sender;
}
else if (_selectBtn !=nil &&_selectBtn == sender){
sender.selected = YES;
} else if (_selectBtn!= sender && _selectBtn!=nil){
_selectBtn.selected = NO;
sender.selected = YES;
_selectBtn = sender;
}
_selectIndex = sender.tag;
}
- (void)layoutPageFrame{
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(10);
make.top.equalTo(self.mas_top).offset(5);
make.height.mas_equalTo(25);
make.width.mas_equalTo(360);
}];
}
- (UILabel *)titleLabel{
if (_titleLabel == nil) {
_titleLabel = [[UILabel alloc]init];
_titleLabel.backgroundColor = [UIColor orangeColor];
_titleLabel.font = [UIFont systemFontOfSize:15.0];
_titleLabel.text = _title;
_titleLabel.textColor = [UIColor blackColor];
}
return _titleLabel;
}
- (UIButton *)selectBtn{
if (_selectBtn == nil) {
if (_items>0) {
for (int i = 0; i <_items.count; i ++) {
_selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_selectBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[_selectBtn setTitle:[_items objectAtIndex:i] forState:UIControlStateNormal];
[_selectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
_selectBtn.backgroundColor = [UIColor whiteColor];
[_selectBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//单选用到的
[_selectBtn setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[_selectBtn setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
CGFloat tipDescW =95.0;
CGFloat tipDescH =30.0;
int totalColumns = 3;//
CGFloat marginX = (365-totalColumns*tipDescW)/(totalColumns +1);
int row = i/totalColumns;
int col = i%totalColumns;
_selectBtn.frame = CGRectMake(marginX+ col*(marginX + tipDescW), 40+(row* tipDescH)+(row * 20), tipDescW, tipDescH);
_selectBtn.tag =i;
[self addSubview:_selectBtn];
[_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
}
return _selectBtn;
}
@end
用的时候在Controller里面 直接使用就可以了
NSArray * items = @[@"1人",@"2人",@"3人",@"四人",@"5人"];
SelectView * selectView = [[SelectView alloc]initWithTitle:@"入住人数" andItemArrar:items];
CGFloat selectViewH = (items.count/3+1) * 60 + 40;
selectView.frame = CGRectMake(0, 150, self.view.frame.size.width, selectViewH);
[self.view addSubview:selectView];