ios - UIImageView 在 Xcode8 和 Swift3 中被隐藏

标签 ios swift xcode uitableview

我的问题很简单:如果我尝试通过 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/

相关文章:

ios - 按下主页按钮时如何使ios应用程序停止

ios - 使用 If 语句的 ios 开发是否会影响性能?

json - 如何访问数组中同名的 JSON 变量?

ios - 使用自动布局调整 xib 元素的宽度

ios - 应用于 CGPath 的比例变换会影响渲染质量吗?

objective-c - 在 iOS 中使用 Obj-C 解析文本文件

ios - tapGestureRecognizer 在 UIScrollView Swift 上不触发 Action

xcode - Xcode 4 是否已准备好用于 iOS 开发或仍处于测试阶段?

iphone - iPhone 上的自动化接口(interface)测试

ios - glTexImage2D 读取超出缓冲区范围(iOS)