Search code examples
iosobjective-cuiimageviewuipickerview

Unable to set Background only for UiImageView only


Im trying to give a background for an image view only as white. Following is my code.

dataArray = [[NSMutableArray alloc] init];
    [dataArray addObject:@"Knowledge"];
    [dataArray addObject:@"Client"];

    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float pickerWidth = screenWidth * 3 / 4;

    float xPoint = screenWidth / 2 - pickerWidth / 2;

    pickerView = [[UIPickerView alloc] init];
    pickerView.transform = CGAffineTransformMakeScale(1, 1);
    [pickerView setDataSource: self];
    [pickerView setDelegate: self];
    [pickerView setFrame: CGRectMake(xPoint, 200.0f, pickerWidth, 200.0f)];
    pickerView.showsSelectionIndicator = YES;
    [pickerView selectRow:2 inComponent:0 animated:YES];

    UIImageView *imageview = [[UIImageView alloc]
                              initWithFrame:CGRectMake(30, 20, 300, 400)];
    [imageview setImage:[UIImage imageNamed:@"logo.png"]];
    [imageview setContentMode:UIViewContentModeScaleAspectFit];
    [imageview setBackgroundColor: [UIColor whiteColor]];
    [self.view addSubview:imageview];
    [self.view addSubview: pickerView];

Im facing two problems while doing so,

  1. The background colour is being applied for both UIImageView and UiPickerView thought I have specified specifically as [imageview setBackgroundColor: [UIColor whiteColor]]; when it has to be only for UiImage.
  2. The alignment changes when the phone is in landscape mode though I have given a proper coordinates. How can I sort this out?

Solution

  • Set pickerview's background color like,

       pickerView.backgroundColor = [UIColor lightGrayColor]; //desired color
    

    hope this will help :)