Cocoa Touch - 以编程方式添加 UIImageView?
-
28-09-2020 - |
题
如何在 Objective-C 中创建和定位新的图像视图?
谢谢!
我尝试过这个,但似乎没有任何作用......
-(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];
我将此方法放置在我的视图控制器中。我看到一些 CG 内容,是否需要导入核心显卡或其他内容?(到底什么是核心显卡?)
解决方案
您在询问iPhone图像视图。正确的?然后尝试这个
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
.
这将创建100 x 50维度的图像视图,并定位父视图的(10,10)。
检查 uiimageview uiview 。您可以使用 imageview的图像属性。
imgView.image = [UIImage imageNamed:@"a_image.png"];.
,您可以使用 UIView的ContentMode 属性,配置如何适合视图中的图像。例如,您可以使用
imgView.contentMode = UIViewContentModeCenter以将所需图像放置到视图的中心。可用的contentmodes列出这里
其他提示
您尚未将视图添加为subview到另一个视图,这意味着它不在视图层次结构中。
假设您在视图控制器中执行此操作,它可能看起来像:
[self.view addSubview: imgView];
. (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:@"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}
. 我在传递值时遇到问题 NSSArray
到 NSString
,
我得到了答案:
//Display all images....
NSString *imgstring= [self.allimages objectAtIndex:indexPath.row];
cell.myimages.image=[UIImage imageNamed:imgstring]; //displaying Images in UIImageView..noproblem.
// Display all numbers...
cell.mylables.text=[NSString stringWithFormat:@"%@", [mysetobjectAtIndex:indexPath.row ]]; //showing results fine ArghyA..
不隶属于 StackOverflow