gpt4 book ai didi

cocoa-touch - Cocoa Touch - 以编程方式添加 UIImageView?

转载 作者:行者123 更新时间:2023-12-03 14:26:45 25 4
gpt4 key购买 nike

如何在 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];

我将此方法放在我的 View Controller 中。我看到一些 CG 的东西,我需要导入核心图形还是什么? (无论如何,核心图形是什么?)

最佳答案

您在询问 iPhone ImageView 。对?然后试试这个

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

这将创建一个 100 x 50 尺寸的 ImageView 并定位 (10, 10) 父 View 。

查看 UIImageView 的引用手册和 UIView .您可以使用 image 设置图像 ImageView 的属性。

imgView.image = [UIImage imageNamed:@"a_image.png"];

您可以使用 contentMode UIView 的属性来配置如何使图像适合 View 。例如,您可以使用

imgView.contentMode = UIViewContentModeCenter 将所需的图像放置到 View 的中心。列出了可用的 contentModes here

关于cocoa-touch - Cocoa Touch - 以编程方式添加 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3321374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com