创buildUIImageView

如何在代码中创buildUIImageView ,而不是在我的xib文件中创build它?

 UIImageView *someImageView = [[UIImageView alloc] initWithFrame:someRect]; someImageView.image = someImage; [self.view addSubview:someImageView]; 

易如反掌! :d

您可能也想检查苹果文档: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

你可以单独创build一个UIImage,并从你的UIImage创build你的UIImageView。

 UIImage *myImage = [UIImage imageNamed:@"great_pic.png"]; UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; 

然后,设置你的UIImageView的大小

 [myImageView setFrame:CGRectMake(0, 0, 100, 200)]; 

做任何你想要的形象,但不要忘记释放它。

 [anotherView addSubview:myImageView]; [myImageView release]; 

到目前为止的答案是比他们需要更复杂:initWithImage:“调整接收器的框架,以匹配指定的图像的大小。”所以你所需要的是

 UIImageView* v = [[UIImageView alloc] initWithImage: someImage]; 
 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 7, 190, 23]; tabNameBackGround.image = [UIImage imageNamed:@"a.png"]; [self.view addSubview:imageView];