3

I created a decimal number UIPickerView. I want the second component which is just a constant dot string (.) became as a separator, I mean :

1) It doesn't move

2) Its width is smaller than other components

3) Its color is different in comparison with other components

enter image description here

Here is the code:

#pragma mark - UIPickerView : Datasource Protocol
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {    if(component == 2)
        return 10;
    if(component == 1)
        return 1;
    else
        return 1000;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == 1)
        return @".";
    else
        return [NSString stringWithFormat:@"%ld", (long)row];

}

How can I do this?

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Soheil Novinfard
  • 1,358
  • 1
  • 16
  • 43
  • 2
    You might want to subclass the picker view and add the dot view (a label maybe) as a subview. I'd position it - and bring it to front - in an overriden `layoutSubviews` method. – Rok Jarc Oct 04 '16 at 08:33
  • Try having 2 component `UIPickerView` and add a `UILabel` above that `UIPickerView`. – Mrunal Oct 04 '16 at 09:05

1 Answers1

1

You can take a UILable and set the text as "." (i.e Dot), add the label in the picker view. Then take 3 components in the relevant delegate and for second component return nil.

Here is the Code :

In your designing method, I did it in 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.font = [UIFont boldSystemFontOfSize:40];
label.layer.cornerRadius = 18.0;
label.layer.masksToBounds = YES;
label.text = @".";
[label setTextColor:[UIColor darkGrayColor]];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[myPickerView addSubview:label];

Then in the delegate would be like this

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component ==0) {
        return self.arrItemsTop.count;
    }else if (component == 1){
        return 0;
    }else
        return self.arrItemsOther.count;

}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if (component == 0) {
        return self.arrItemsTop[row];
    }
    else if (component == 1){
        return nil;
    }
    else{
        return self.arrItemsOther[row];
    }
}

NB : arrItemsTop is the array holding left side values and arrItemsOthers is the array holding right side value

OutPut :

enter image description here

Hope it helps ..

Happy coding .

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43