xcode 如何创建一个动态的 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3178758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a dynamic UIImageView
提问by Jonathan
I want to create an UIImageView with, for example, "Trend.jgp" for a special case (if-condition). I tried various ways with CGRectMake but I am not able to create this picture dynamically to a special place. Furthermore, I can't use the function setimage like it's described in the Developer Documentation.
我想为特殊情况(if-condition)创建一个 UIImageView ,例如“Trend.jgp”。我用 CGRectMake 尝试了各种方法,但我无法将这张图片动态创建到一个特殊的地方。此外,我不能像开发人员文档中描述的那样使用 setimage 函数。
Has anyone got an idea how to realise my plan?
有没有人知道如何实现我的计划?
回答by Eiko
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Trend"]];
imageView.frame = CGRectMake(....) or imageView.center = CGPointMake(...)
Add this view to your container view, i.e. [myView addSubview:imageView];
将此视图添加到您的容器视图中,即 [myView addSubview:imageView];
回答by Jonathan
UIImageView *imageView = nil;
if (specialCase) {
imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"Trend.jpg"]];
} else {
//whatever you want to do in the other case
}