将 PHLivePhotoView 添加到故事板

Add PHLivePhotoView to the storyboard

我正在尝试使用 Storyboard 将 PHLivePhotoView 放入 WelcomeVideoItemViewController。为此,配置了自定义 class 的视图:

场景层级:

自定义视图控制器的实现:

class WelcomeVideoItemViewController: UIViewController, PHLivePhotoViewDelegate {
    @IBOutlet weak var livePhotoView: PHLivePhotoView!

    @IBOutlet weak var tipImageView: UIImageView!

    @IBOutlet weak var tipLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        print(livePhotoView)
        // Optional(<UIView: 0x7fd161f200b0; frame = (0 0; 320 568); autoresize = RM+BM; layer = <CALayer: 0x60000134ac60>>)

        // Crash here
        livePhotoView.delegate = self
    }
}

所以问题是 livePhotoView 是用 UIView 实例初始化的,这导致崩溃,因为 UIView 没有 delegate 属性 .我可以让这个自定义 class 起作用吗?

Xcode10.2,iOS12.2,Swift4.2

我找到了解决方法。首先,创建自定义 PHLivePhotoView subclass:

import PhotosUI

class LivePhotoView: PHLivePhotoView {
}

然后,将 View 拖到故事板并为其设置自定义 class:

别忘了检查 Inherit Module From Target