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
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?
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 :
Hope it helps ..
Happy coding .