how to get Custom UIPickerView

爱⌒轻易说出口 提交于 2019-12-12 08:38:32

问题


Hey Guys Please help me i want to achieve the following functionality in Custom UIPiker As shown in below Picture.

I want to change the text colour of selected area only like above


回答1:


Add lable in your pickerview in your viewDidLoad method as below.

Define label and myPickerView both in ViewController.h file

- (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.
}

in pickerview delegate set label title.

#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];
}

Your OuytPut is :



来源:https://stackoverflow.com/questions/25156986/how-to-get-custom-uipickerview

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