无法仅为 UiImageView 设置背景

Unable to set Background only for UiImageView only

我试图只为图像视图提供白色背景。以下是我的代码。

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];

我在这样做的时候遇到了两个问题,

  1. UIImageView 和 UiPickerView 都应用了背景色,我已经明确指定为 [imageview setBackgroundColor: [UIColor whiteColor]];当它必须只用于 UiImage 时。
  2. 当 phone 处于横向模式时对齐方式会发生变化,尽管我已经给出了正确的坐标。我该如何解决这个问题?

像这样设置pickerview的背景颜色,

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

希望这会有所帮助:)

试试这个代码:

imageView.backgroundColor = [UIColor clearColor];
imageView.opaque = NO;

试试这个:-

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
[self.view addSubview:backgroundView];