項目需要做一個下拉框特恬,根據選擇內容的不同换棚,加載不同的視圖酝陈,在安卓開發(fā)工具里有現成的工具 spinner营罢,直接拿來用就可以了,而蘋果沒有仲智,于是自己模仿了一個
先看效果圖:
思路 :一個imageView 上面添加 button 和line 张足,button 根據tag 值來顯示不同的內容,最后可以在buttonAction里面進行所需要的操作
_array = @[@"請選擇內容",@"發(fā)起群聊",@"添加朋友",@"掃一掃",@"收款",@"全部交易",@"失敗交易",@"成功交易",@"待審核交易"];
[self showChooseView: _array];
分裝了一個方法,坎藐,需要傳入一個內容數組:
- (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);
}];
}
}
}
附上 圖片:
具體可以見我的demo :
http://pan.baidu.com/s/1qYbToDY 密碼 i34e