-(UIImage*)imageForCountryCode:(NSString*)countryCode
{
NSString *imagePath = [NSString stringWithFormat:@"CountryPicker.bundle/%@", countryCode];
UIImage *image;
if ([[UIImage class] respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)])
{
image = [UIImage imageNamed:imagePath inBundle:[NSBundle bundleForClass:[CountryPicker class]] compatibleWithTraitCollection:nil];
}
else
{
image = [UIImage imageNamed:imagePath];
}
return image;
}
So by subclassing, one can add flags for extra added nations. I needed to add nations such as Soviet Union to collect people's country of birth.
- (UIView *)pickerView:(__unused UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(__unused NSInteger)component reusingView:(UIView *)view
{
if (!view)
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 30)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35, 3, 245, 24)];
label.backgroundColor = [UIColor clearColor];
label.tag = 1;
if (self.labelFont)
{
label.font = self.labelFont;
}
[view addSubview:label];
UIImageView *flagView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 24, 24)];
flagView.contentMode = UIViewContentModeScaleAspectFit;
flagView.tag = 2;
[view addSubview:flagView];
}
((UILabel *)[view viewWithTag:1]).text = [[self class] countryNames][(NSUInteger)row];
UIImage *image = [self imageForCountryCode:[[self class] countryCodes][(NSUInteger) row]];
((UIImageView *)[view viewWithTag:2]).image = image;
return view;
}