如何获得自定义的UIPickerView

嘿,伙计们请帮助我,我想在自定义UIPiker中实现以下function如下图所示。

在这里输入图像说明

我只想像上面那样改变选定区域的文字颜色

在你的viewDidLoad方法的pickerview中添加lable,如下所示。

ViewController.h文件中定义labelmyPickerView

 - (void)viewDidLoad { myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; myPickerView.delegate = self; myPickerView.showsSelectionIndicator = YES; [self.view addSubview:myPickerView]; label = [[UILabel alloc] initWithFrame:CGRectMake(145, 76, 36, 36)]; //label.text = @"Label"; label.font = [UIFont boldSystemFontOfSize:20]; label.layer.cornerRadius = 18; [label setTextColor:[UIColor whiteColor]]; label.backgroundColor = [UIColor blueColor]; label.textAlignment = NSTextAlignmentCenter; label.shadowColor = [UIColor whiteColor]; label.shadowOffset = CGSizeMake (0,1); [myPickerView addSubview:label]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } 

在pickerview委托设置标签标题。

 #pragma mark - PickerView delegate - (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component { // Handle the selection [label setText:[NSString stringWithFormat:@"%d",row]]; NSLog(@"%@",[NSString stringWithFormat:@"%d",row]); } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSLog(@"%@",[NSString stringWithFormat:@"%d",row]); [label setText:[NSString stringWithFormat:@"%d",row]]; return [NSString stringWithFormat:@"%d",row]; } 

你的OuytPut是:

在这里输入图像说明