添加为子视图时,新创建的UIImageView不会显示
-
25-10-2019 - |
题
点击时,我想让一些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,它使用接口构建器创建以包含其他视图和控件。
尽管如此,什么都看不到...。
有人知道我在做什么错吗?我需要设置Alpha属性(或其他属性)吗?
谢谢
Zuppa
解决方案
我会检查几件事:
- 登录以确保实际上调用了这一部分的代码部分。
使用指定的初始化器
UIImageView
:[[UIImageView alloc] initWithImage:image];
这将确保您的新视图具有适合图像的正确尺寸 -
zap
还有你的形象?它可能不在框架之外。确保图像实际存在并创建
尝试不调整图层属性,而是设置
center
相反的图像视图。实际上,首先不要对其进行调整,只需查看您是否可以看到任何东西。我不确定,但我想position
可能正在移动层 内 因此,视图可能会将您的图像视为视线。尝试:CGPoint newCenter = that.frame.origin; newCenter.y += zap.bounds.size.height/2; newCenter.x += i*20; zap.center = origin;
setNeedsDisplay
不需要。显然,从您的评论中,这是一种脱颖而出的行为!
其他提示
您的uiimage nil吗?您不需要有.png扩展 imageNamed:
去工作。我不确定,它甚至可能无法正常工作。您也推出图像。 imageNamed:
返回自我释放的图像,因此没有理由在图像上拨打释放,除非您还在某个地方打电话保留。
我看到一个潜在的错误:在第二个调用中,您将重新添加另一副本,因此请注意在添加Enew副本之前删除子视图。 Yu可以或者可以移动先前的视图。
我有99%的案例关于图像的不可访视视图是错误的名称,因此,如建议的那样,使用温度var加载:
uiimage img = [Uiimage成像..];
和测试IMG。
不隶属于 StackOverflow