简体   繁体   English

UIImageView的图像已从代码更改,不再居中

[英]Changed image of UIImageView from code, not centered anymore

So I just spent like one hour Googling for some answers. 所以我只花了一个小时就在Google上搜寻了一些答案。

  1. I added UIImageView with Interface Builder and I set image to some existing image. 我在Interface Builder中添加了UIImageView,并将图像设置为一些现有图像。
  2. I wanted to make the image centered, so I changed mode to centered. 我想使图像居中,所以我将模式更改为居中。 Worked. 工作。
  3. Now I wanted to change the image from code. 现在我想从代码中更改图像。 So I set image in IB to nothing and I did imageView.image = (UIImage instance) 所以我将IB中的图像设置为空,然后执行了imageView.image = (UIImage instance)

Now the image changed, but it's not centered anymore. 现在图像已更改,但不再居中。

Also, here's an interesting thing: I tried also not changing image in IB, I just left it set to some image and then I changed image from code. 另外,这是一件有趣的事情:我也尝试不更改IB中的图像,只是将其设置为某些图像,然后从代码中更改了图像。 I could see both images! 我可以看到两个图像! The one set in IB was centered (like I want it) and the new one was overlaying on the left. IB中的一组居中(如我所愿),而新组则覆盖在左侧。

Any ideas please? 有什么想法吗?

Also I probably lack some basic knowledge of this View stuff, can anyone please point me at documentation where this is explained? 另外,我可能还缺少一些有关View的基本知识,有人可以向我指出解释此内容的文档吗?

It sounds like you have two overlapping UIImageView subviews. 听起来您有两个重叠的UIImageView子视图。 One of them is referenced by imageView in your view controller and doesn't center. 其中之一是由视图控制器中的imageView引用的,并且不会居中。 The other isn't referenced by your view controller but does center. 另一个未被您的视图控制器引用,但居中。 The solution would be to get rid of the non-referenced image view. 解决的办法是摆脱未引用的图像视图。

I got it! 我知道了! Thanks James Huddleston for useful comments. 感谢James Huddleston的有用评论。

You were right. 你是对的。 There must have been to UIImageViews. 一定要去过UIImageViews。

Here's what happened. 这是发生了什么事。 I actually used viewWithTag: to find that view (it wasn't linked directly - it was in a UITableViewCell). 我实际上使用viewWithTag:查找该视图(它没有直接链接-在UITableViewCell中)。 Well, it apparently didn't work with 0, when I changed tag to 1 - it worked. 好吧,当我将tag更改为1时,它显然不能与0一起工作-它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM