[英]UIImageView gets hidden in Xcode8 and Swift3
My problems is simple: My @IBOutlet UIImageView in my custom UITableViewCell gets hidden if I try to access it via the cellForRowAtIndexpath method. 我的问题很简单:如果我尝试通过cellForRowAtIndexpath方法访问它,那么自定义UITableViewCell中的@IBOutlet UIImageView将被隐藏。
I've heard that this is either a Swift 3 or Xcode 8 problem (which makes sense because I just now have this problem after updating). 我听说这是Swift 3或Xcode 8问题(这很有意义,因为我刚才在更新后遇到了这个问题)。 I had the same problem with a UIImageView and discovered that the reason it was getting hidden was because I was calling it too early in the cycle.
我对UIImageView遇到了同样的问题,发现它被隐藏的原因是因为我在周期中太早调用了它。 Since the latest update, if I try to access an @IBOutlet from the nib, I can only do so in the viewDidAppear method.
从最新的更新开始,如果尝试从笔尖访问@IBOutlet,则只能在viewDidAppear方法中进行。 If I try in the viewDidLoad or viewWillLoad method, then the outlet gets hidden.
如果我尝试使用viewDidLoad或viewWillLoad方法,则插座将被隐藏。
In this case, I am simply changing a UIImageView from a square to a circle via the following two lines of code: 在这种情况下,我只是通过以下两行代码将UIImageView从正方形更改为圆形:
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2;
cell.pictureImageView.clipsToBounds = true;
Again, this works only the vieDidAppear method. 同样,这仅适用于vieDidAppear方法。 Is there a viewDidAppear method for a UITableViewCell?
UITableViewCell是否有viewDidAppear方法? I put the same two lines in the cellForRowAtIndexPath and the image goes away:
我在cellForRowAtIndexPath中放置了相同的两行,图像消失了:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.Cell, for: indexPath) as! TimelineTableViewCell
//Turn profile picture into a circle
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2;
cell.pictureImageView.clipsToBounds = true;
return cell
}
I also tried it in the custom cell's awakeFromNib method and the same thing happened...the image disappears: 我也在自定义单元格的awakeFromNib方法中尝试过,发生了同样的事情……图像消失了:
override func awakeFromNib() {
super.awakeFromNib()
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2;
cell.pictureImageView.clipsToBounds = true;
}
Any help is much appreciated, thanks everyone! 非常感谢任何帮助,谢谢大家!
Cheers, 干杯,
C C
You are calling it too early. 您称呼为时过早。
pictureImageView
has no known width
yet. pictureImageView
尚不知道width
。 You need to call layoutIfNeeded
: 您需要调用
layoutIfNeeded
:
cell.pictureImageView.layoutIfNeeded()
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2
cell.pictureImageView.clipsToBounds = true
The circle is: 圆圈是:
init
UIViewController awakeFromNib
loadView // your pictureImageView is loaded here
UIView awakeFromNib
UIViewController viewDidLoad
viewWillAppear
viewDidAppear // the properties of your pictureImageView are available here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.