我的问题很简单:如果我尝试通过 cellForRowAtIndexpath 方法访问它,我的自定义 UITableViewCell 中的 @IBOutlet UIImageView 会被隐藏。
我听说这是 Swift 3 或 Xcode 8 的问题(这是有道理的,因为我刚刚在更新后遇到了这个问题)。我在使用 UIImageView 时遇到了同样的问题,发现它被隐藏的原因是因为我在循环中调用它的时间过早。自最新更新以来,如果我尝试从 Nib 访问 @IBOutlet,我只能在 viewDidAppear 方法中这样做。如果我在 viewDidLoad 或 viewWillLoad 方法中尝试,则导出会被隐藏。
在这种情况下,我只是通过以下两行代码将 UIImageView 从正方形更改为圆形:
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2;
cell.pictureImageView.clipsToBounds = true;
同样,这仅适用于 vieDidAppear 方法。 UITableViewCell 是否有 viewDidAppear 方法?我将相同的两行放在 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
}
我也在自定义单元格的 awakeFromNib 方法中尝试过,同样的事情发生了……图像消失了:
override func awakeFromNib() {
super.awakeFromNib()
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2;
cell.pictureImageView.clipsToBounds = true;
}
非常感谢任何帮助,谢谢大家!
干杯,
C
最佳答案
你说得太早了。 pictureImageView
还没有已知的宽度
。
您需要调用 layoutIfNeeded
:
cell.pictureImageView.layoutIfNeeded()
cell.pictureImageView.layer.cornerRadius = cell.pictureImageView.frame.size.width / 2
cell.pictureImageView.clipsToBounds = true
圆圈是:
init
UIViewController awakeFromNib
loadView // your pictureImageView is loaded here
UIView awakeFromNib
UIViewController viewDidLoad
viewWillAppear
viewDidAppear // the properties of your pictureImageView are available here
关于ios - UIImageView 在 Xcode8 和 Swift3 中被隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39669325/