繁体   English   中英

UIImageView被隐藏在Xcode8和Swift3中

[英]UIImageView gets hidden in Xcode8 and Swift3

我的问题很简单:如果我尝试通过cellForRowAtIndexpath方法访问它,那么自定义UITableViewCell中的@IBOutlet UIImageView将被隐藏。

我听说这是Swift 3或Xcode 8问题(这很有意义,因为我刚才在更新后遇到了这个问题)。 我对UIImageView遇到了同样的问题,发现它被隐藏的原因是因为我在周期中太早调用了它。 从最新的更新开始,如果尝试从笔尖访问@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尚不知道width 您需要调用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

暂无
暂无

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

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