iOS 下拉框

2017-09-12  本文已影响51人  我叫山鸡_

项目需要做一个下拉框,根据选择内容的不同,加载不同的视图,在安卓开发工具里有现成的工具 spinner,直接拿来用就可以了,而苹果没有,于是自己模仿了一个

先看效果图:
效果图.gif

思路 :一个imageView 上面添加 button 和line ,button 根据tag 值来显示不同的内容,最后可以在buttonAction里面进行所需要的操作

   _array = @[@"请选择内容",@"发起群聊",@"添加朋友",@"扫一扫",@"收款",@"全部交易",@"失败交易",@"成功交易",@"待审核交易"];
    
   [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);
                
                
            }];
            
        }
        
    }


}

附上 图片:

tanchukuang@2x.png
具体可以见我的demo :
http://pan.baidu.com/s/1qYbToDY 密码 i34e
上一篇下一篇

猜你喜欢

热点阅读