iosuipicker

Custom uipicker- dealing with image size


enter image description here

-(UIView *)pickerView:(UIPickerView *)pickerView
           viewForRow:(NSInteger)row
         forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //NSLog(@"%d",component);
   // NSLog(@"tag: %d",pickerView.tag);

    if([pickerView isEqual:picker1])

    {


        NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",component+1];
        NSArray *array1 = [self valueForKey:arrayName];
        UIImageView *imageView = [array1 objectAtIndex:row];
        if ([pickerView selectedRowInComponent:component] == row) {
            imageView.frame = CGRectMake(60,60,40,80);//set bigger frame
        } else {
            //imageView.frame = CGRectMake(30,30,20,60);//set normal frame
        }
        return imageView;
        }

}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 80.0;

}

I am implementing custom uipicker to achieve the first following image. I have three images to be chosen with uipicker. However, all of them are shown on the screen in the second image below. I want only in middle shown occupy bigger size rather than other images as shown image.

enter image description here

enter image description here

enter image description here


Solution

  • Update: As per your comment, you need to make the middle row to be bigger one.

    Modify your viewForRow method as,

    - (UIView *)pickerView:(UIPickerView *)pickerView
               viewForRow:(NSInteger)row
             forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        //NSLog(@"%d",component);
        NSLog(@"tag: %d",pickerView.tag);
    
        if(pickerView.tag==1)
        {   NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d",component+1];
            NSArray *array1 = [self valueForKey:arrayName];
            UIImageView *imageView = [array1 objectAtIndex:row];
            if ([pickerView selectedRowInComponent:component] == row) {
               imageView.frame = CGRectMake(..);//set bigger frame
            } else {
               imageView.frame = CGRectMake(..);//set normal frame
            }
            return imageView;
        }
    

    Implement pickerView:rowHeightForComponent: method and set suitable height for rows.

        - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    

    In order to hide the Selection Indicator, set showsSelectionIndicator property of UIPickerView. For more details check apple doc.

    Based on your comment,

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
          [pickerView reloadComponent:component];
    }