一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ios开发中UIPickerView使用例子

时间:2022-06-25 23:32:57 编辑:袖梨 来源:一聚教程网

系统的UIPickerView很简单,样式也是很简单单调,界面感觉很单调不怎么好看,有时候就需要我们来自己自定义,做出自己想要的样式。首先给出普通样式的UIPickerView示例,贴上代码:

#import "zidingyipikViewController.h"
 
@interface zidingyipikViewController (){
    //记录滚轮是否滑动
    NSString *guildStr;
    NSString *selectStr;
    NSMutableArray *dataMutArray;
    UIButton *bgButton;
}
 
@end
 
@implementation zidingyipikViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    dataMutArray = [NSMutableArray arrayWithArray:@[@"学生",@"工人",@"教师",@"保安",@"医生",@"护士",@"服务员"]];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 140, 70, 30)];
    [button setTitle:@"弹出框" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
#pragma mark - 弹框
- (void)buttonAction{
    guildStr = @"0";
    bgButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    bgButton.backgroundColor = RGBA(0, 0, 0, 0.3);
    [bgButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bgButton];
    
    UIView *cycanView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 180, kScreenWidth, 40)];
    cycanView.backgroundColor = [UIColor orangeColor];
    [bgButton addSubview:cycanView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, cycanView.height)];
    titleLabel.text = @"选择身份";
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [cycanView addSubview:titleLabel];
    
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, cycanView.height)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = FONT(14);
    [cancelButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cycanView addSubview:cancelButton];
    
    UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 48, 0, 48, cycanView.height)];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.titleLabel.font = FONT(14);
    [confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cycanView addSubview:confirmButton];
    
    UIPickerView *selectPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 140, kScreenWidth, 140)];
    // 显示选中框
    selectPickerView.showsSelectionIndicator = NO;
    selectPickerView.backgroundColor = [UIColor whiteColor];
    selectPickerView.delegate = self;
    selectPickerView.dataSource = self;
    selectPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [bgButton addSubview:selectPickerView];
}
#pragma mark - 隐藏弹框
- (void)bgButtonAction:(UIButton *)sender{
    [bgButton removeFromSuperview];
}
#pragma mark - 弹框确定按钮
- (void)confirmButtonAction:(UIButton *)sender{
    if ([guildStr isEqualToString:@"0"]) {
        selectStr = [NSString stringWithFormat:@"%@",dataMutArray[0]];
    }
    [self showHUDTextOnly:selectStr];
    [bgButton removeFromSuperview];
}
#pragma mark - UIPickerView代理方法
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return dataMutArray.count;
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return kScreenWidth - 85 * 2;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    guildStr = @"1";
    selectStr = [NSString stringWithFormat:@"%@",[dataMutArray objectAtIndex:row]];
    NSLog(@"selectStr:%@",selectStr);
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [dataMutArray objectAtIndex:row];
}

接下来,如果要改变样式,自定义自己需要的UIPickerView样式,只要重写方法就行了。方法:


//重写方法,改变字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = FONT(17);
        pickerLabel.textColor = [UIColor blackColor];
        pickerLabel.textAlignment = 1;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    //在该代理方法里添加以下两行代码删掉上下的黑线
    [[pickerView.subviews objectAtIndex:1] setHidden:YES];
    [[pickerView.subviews objectAtIndex:2] setHidden:YES];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(85, 55, kScreenWidth - 85 * 2, 1.8)];
    lineView1.backgroundColor = RGB(245, 245, 245);
    [pickerView addSubview:lineView1];
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(85, 82, kScreenWidth - 85 * 2, 1.8)];
    lineView2.backgroundColor = RGB(245, 245, 245);
    [pickerView addSubview:lineView2];
    
    return pickerLabel;
}
注:
// 当前屏幕宽度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 当前屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

热门栏目