0

I am not using the UIBuilder. I have programmatically added two subviews to the main view. I can see them if I add background color to them. However, I want to set the value of the two subviews to images.

@property (strong, nonatomic) IBOutlet UIView *firstImage;
@property (strong, nonatomic) IBOutlet UIView *secondImage;
.
.
.
self.firstImage = [[UIView alloc] initWithFrame:CGRectMake(3, 89, 156, 78)];
[self.view addSubview:self.firstImage];

self.secondImage = [[UIView alloc] initWithFrame:CGRectMake(161, 89, 156, 78)];
[self.view addSubview:self.secondImage];
.
.
.
self.leftView.image = self.firstImage;
self.rightView.image = self.secondImage;

My images are not displaying. Is there something I need to do to hook them up since I'm not using the UIBuilder? I am grateful for any ideas?

Thank you.

4

2 回答 2

0

您的代码应如下所示:

正确声明您的属性。它们应该属于UIImageUIImageView类,因为您要显示图像。

@property (strong, nonatomic) UIImage *firstImage;
@property (strong, nonatomic) UIImage *secondImage;

@property (strong, nonatomic) UIImageView *leftImageView;
@property (strong, nonatomic) UIImageView *rightImageView;

仔细初始化您的图像,输入图像的确切名称。-imageNamed: 对名称区分大小写。

self.firstImage = [UIImage imageNamed:@"firstImage.png"];
self.secondImage = [UIImage imageNamed:@"secondImage.png"];

设置应该我们的实例leftImageView显示和框架的初始图像,将其添加为我们的子视图self.view

self.leftImageView = [UIImageView alloc]initWithImage:self.firstImage];
self.leftImageView.frame = CGRectMake(3, 89, 156, 78);
[self.view addSubview:self.leftImageView];

与第二张图像相同的操作。

self.rightImageView = [UIImageView alloc]initWithImage:self.secondImage];
self.rightImageView.frame = CGRectMake(161, 89, 156, 78)];
[self.view addSubview:self.rightImageView];
于 2013-11-07T20:14:05.643 回答
0

我想 leftView 和 rightView 是 UIImageView 实例。但 firstImage 和 secondImage 是 UIView 实例。

您需要创建 UIImage 的 firstImage 和 secondImage 实例并按如下方式初始化它们。

firstImage = [UIImage imageNamed:@"imageName.jpg"];

在此之后,而不是[self.view addSubView:]你必须做self.leftView.image = firstImage

最后你可以添加[self.view addSubview:self.leftView];

对右视图和第二个图像执行相同的操作,如果您可以显示视图层次结构或您想要的布局,那么您就完成了

于 2013-11-07T20:12:00.020 回答