項(xiàng)目需要做一個(gè)下拉框,根據(jù)選擇內(nèi)容的不同,加載不同的視圖,在安卓開發(fā)工具里有現(xiàn)成的工具 spinner,直接拿來用就可以了,而蘋果沒有,于是自己模仿了一個(gè)
先看效果圖:

效果圖.gif
思路 :一個(gè)imageView 上面添加 button 和line ,button 根據(jù)tag 值來顯示不同的內(nèi)容,最后可以在buttonAction里面進(jìn)行所需要的操作
_array = @[@"請(qǐng)選擇內(nèi)容",@"發(fā)起群聊",@"添加朋友",@"掃一掃",@"收款",@"全部交易",@"失敗交易",@"成功交易",@"待審核交易"];
[self showChooseView: _array];
分裝了一個(gè)方法,,需要傳入一個(gè)內(nèi)容數(shù)組:
- (void)showChooseView:(NSArray *)titleArray{
_mainView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tanchukuang@2x.png"]];
_mainView.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:_mainView];
_mainView.userInteractionEnabled =YES;
_mainView.hidden =YES;
[_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(200);
make.size.mas_equalTo(CGSizeMake(80, titleArray.count*32+10));
make.left.mas_equalTo(140);
}];
for (int i =0; i< titleArray.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[_mainView addSubview:button];
[button setTitle: titleArray[i] forState:UIControlStateNormal];
button.tag =100+i;
[button addTarget:self action:@selector(chooseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:12];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_mainView.mas_top).and.offset(10+30*i);
make.height.mas_equalTo(30);
make.left.right.mas_equalTo(0);
}];
if (i < titleArray.count -1) {
UIView *wayLine = [[UIView alloc]init];
[_mainView addSubview:wayLine];
wayLine.backgroundColor = [UIColor lightGrayColor];
[wayLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_mainView.mas_top).and.offset(10+31*(i+1));
;
make.height.mas_equalTo(1);
make.left.and.right.mas_equalTo(0);
}];
}
}
}
附上 圖片:

tanchukuang@2x.png
具體可以見我的demo :
http://pan.baidu.com/s/1qYbToDY 密碼 i34e