如何在objective-c中创建和定位新的imageview?
谢谢!
我试过这个,但它似乎没有做任何事情......
-(void)drawStars{ //uses random numbers to display a star on screen
//create random position
int xCoordinate = arc4random() % 10;
int yCoordinate = arc4random() % 10;
UIImageView *starImgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, 58, 40)]; //create ImageView
starImgView.image = [UIImage imageNamed:@"star.png"];
[starImgView release];
Run Code Online (Sandbox Code Playgroud)
我把这个方法放在我的viewcontroller中.我看到一些CG东西我需要导入核心图形或其他什么?(无论如何,核心图形是什么?)
tas*_*oor 60
你问的是iPhone图像视图.对?然后尝试这个
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
Run Code Online (Sandbox Code Playgroud)
这将创建100 x 50维度的图像视图并定位父视图的(10,10).
查看UIImageView和UIView的参考手册.您可以使用imageView的图像属性设置图像.
imgView.image = [UIImage imageNamed:@"a_image.png"];
您可以使用UIView 的contentMode属性来配置如何在视图中拟合图像.例如,您可以使用
imgView.contentMode = UIViewContentModeCenter将所需图像放置在视图的中心.此处列出了可用的contentModes
Dar*_*mas 22
您尚未将视图作为子视图添加到另一个视图,这意味着它不在视图层次结构中.
假设您在视图控制器中执行此操作,它可能看起来像:
[self.view addSubview: imgView];
Run Code Online (Sandbox Code Playgroud)