在 ViewController设置UIImageView框架

对于一般的程序,怎么样在 ViewController设置UIImageView框架?

UIView *baseView = [[UIView alloc] init];

[self.view addSubview:baseView];

// 显示UIImageView

UIImageView *myImage = [[UIImageView alloc]
                        initWithImage:[UIImage imageNamed:@"tool.png"]];
[baseView addSubview:myImage];

运行之后就黑屏,我猜是需要设置框架。

你说对了,是需要框架,你可以试试下面的:

UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];

// 显示UIImageView

UIImageView *myImage = [[UIImageView alloc]
                    initWithImage:[UIImage imageNamed:@"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];

同时还有对baseViewmyImage设置autoresizingmask`,这样当视图的框架改变时,这两个视图也自动调整。

如果没用ARC,记得要在方法结束之前自动释放这些视图

不明白你创建UIView的目的是什么?如果想把UIImageView添加到当前UIViewcontroller的view中可以直接

> UIImageView *myImage = [[UIImageView alloc]
>                     initWithImage:[UIImage imageNamed:@"tool.png"]]; myImage.frame = CGRectMake(...); [self.view addSubview:myImage];

这个不需要什么框架之类的东西,你的问题在于没有设置uiview和uiimageview的大小,就是frame。设置了,就肯定好了。