iOS 下拉框

項(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容