将UIImage视图作为子视图添加到UIView的实例

Dan*_*wan 14 objective-c uiimageview addsubview ios

我正在练习初学者代码,因为我是新手,我在这里遇到了很多困惑......这就是我到目前为止所拥有的

UIView *catView = [[UIView alloc] init];
UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[catView.view addSubview:imageView];
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这里的东西是错的,有人可以帮忙吗?

sor*_*god 28

//You need to specify the frame of the view   
UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)];

UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;

// add the imageview to the superview
[catView addSubview:imageView];

//add the view to the main view

[self.view addSubview:catView];
Run Code Online (Sandbox Code Playgroud)