点击后,我想让一些 UIImageView 对象出现在底层 UIView 上。 所以我创建了以下操作:
- (IBAction)startStars: (id) sender
{
UIView* that = (UIView*) sender; // an UIButton, invisible, but reacting to Touch Down events
int tag = that.tag;
UIView* page = [self getViewByTag:mPages index:tag]; // getting a page currently being viewed
if ( page != nil )
{
int i;
UIImage* image = [UIImage imageNamed:@"some.png"];
for ( i = 0 ; i < 10 ; ++i )
{
// create a new UIImageView at the position of the tapped UIView
UIImageView* zap = [[UIImageView alloc] initWithFrame:that.frame];
// assigning the UIImage
zap.image = image;
// make a modification to the position so we can see all instances
CGPoint start = that.layer.frame.origin;
start.x += i*20;
zap.layer.position = start;
[page addSubview:zap]; // add to the UIView
[zap setNeedsDisplay]; // please please redraw
[zap release]; // release, since page will retain zap
}
[image release];
}
}
不幸的是,什么也没有出现。代码被调用,对象被创建,图像被加载,甚至属性也如预期的那样。 页面本身是一个真正的基本 UIView,使用界面生成器创建以包含其他 View 和控件。
尽管如此,还是看不到任何东西......
有人知道我做错了什么吗?我需要设置 alpha 属性(或其他属性)吗?
谢谢
祖帕
最佳答案
我要检查的几件事:
- 记录日志以确保这部分代码确实被调用。
为
UIImageView
使用指定的初始值设定项:[[UIImageView alloc] initWithImage:image];
这将确保您的新 View 具有正确的图像尺寸 -
zap
和您的图像的相对尺寸是多少?它可能超出了框架。确保图像实际存在且已创建
尝试不调整图层属性,而是设置 ImageView 的
中心
。事实上,首先不要调整它,只是看看是否能看到任何东西。我不确定,但我认为position
可能会移动 View 内的图层,因此可能会将您的图像移出视线。尝试:CGPoint newCenter = that.frame.origin; newCenter.y += zap.bounds.size.height/2; newCenter.x += i*20; zap.center = origin;
setNeedsDisplay
不是必需的。从你的评论来看,显然这是一种绝望的行为!
关于ios - 添加为 subview 时,新创建的 UIImageView 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8302923/