简体   繁体   English

UIImageView被隐藏在Xcode8和Swift3中

[英]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.

相关问题 iOS Swift3 XCode8 CoreData - ios swift3 xcode8 coredata UIImagePickerController 使应用程序崩溃 | Swift3,Xcode8 - UIImagePickerController crashes app | Swift3, Xcode8 Swift3和Xcode8:执行功能后无法执行Segue - Swift3 and Xcode8: Not able to perform segue after execution of a function Xcode8:为什么 Xcode8 在转换为 Swift3 语法后没有构建/编译 - Xcode8 : Why Xcode8 is not building/compiling after converting to Swift3 syntax's 将 CMTime 保存在核心数据 + Xcode8 + swift3 - save CMTime in core data + Xcode8 + swift3 强制执行顺序-Swift3 / Xcode8-线程 - Force execution order - Swift3/Xcode8 - Threads urlSession:dataTask:didReceive:在Xcode8 Swift3中没有调用completionHandler - urlSession:dataTask:didReceive:completionHandler not called in Xcode8 Swift3 用于捕获QR代码的代码在Xcode8和Swift3中似乎无法正常工作 - Code for capturing QR Code doesn't seem working well in Xcode8 and Swift3 XCode8 Swift3 - 打开联系人列表并通过单击检索数据的问题 - XCode8 Swift3 - Problems with open contact list and retrieve data by click 下拉刷新时,UITableview中发生错误,所有数据重复自身。 使用Swift3,Xcode8可以帮助任何人吗 - Error in UITableview when pull down to refresh, all data repeating itself. Can any one help using Swift3 , Xcode8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM