How to get Custom UIPickerView?
Asked Answered
P

1

6

I want to achieve the following functionality in Custom UIPiker as shown in below picture.

enter image description here

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

Prerecord answered 6/8, 2014 at 9:40 Comment(2)
add this into question not in commentTheodore
you have to take different label for different selection. Check it.Cortico
C
7

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 :

enter image description here

Cortico answered 6/8, 2014 at 10:52 Comment(3)
Thanks It Works..... Just 1 thing unable to round the corner of label background, Although using: label.layer.cornerRadius = 18;Prerecord
Your label width and height is same and corner radius is half of width. then you lable is rounded.Cortico
Weird behaviour when you drag it up it changes to 4 and back to 0 even if 4 is in the bottom of the selectionCore

© 2022 - 2024 — McMap. All rights reserved.