UIPickerView自定义显示视图

被刻印的时光 ゝ 提交于 2019-11-30 14:32:33
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.data.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.data[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 70;
}
- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row forComponent:(NSInteger)component
           reusingView:(UIView *)view {
    //普通状态的颜色
    UILabel *normal = (UILabel*)view;
    if (!normal){
        normal = [[UILabel alloc] init];
        normal.textColor = [UIColor lightGrayColor];
        normal.textAlignment = NSTextAlignmentCenter;
        normal.font = [UIFont systemFontOfSize:30];
    }
    normal.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    //当前选中的颜色
    UILabel *select = (UILabel*)[pickerView viewForRow:row forComponent:0];
    if (select) {
        select.textColor = [UIColor redColor];
        select.textAlignment = NSTextAlignmentCenter;
        select.font = [UIFont systemFontOfSize:60];
    }
    
    //下一个选中的颜色
    UILabel *selectNext = (UILabel*)[pickerView viewForRow:row + 1 forComponent:0];
    if (selectNext) {
        selectNext.textColor = [UIColor lightGrayColor];
        selectNext.textAlignment = NSTextAlignmentCenter;
        selectNext.font = [UIFont systemFontOfSize:30];
    }
    
    //设置分割线
    for (UIView *line in pickerView.subviews) {
        if (line.frame.size.height < 1) {//0.6667
            line.backgroundColor = [UIColor clearColor];
        }
    }
    
    return normal;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!