将PHLivePhotoView添加到情节提要

时间:2019-04-04 07:18:59

标签: ios swift xcode storyboard

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

场景层次:
Hierarchy of a ViewController

自定义View Controller的实现:

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
    }
}

因此,问题在于livePhotoViewUIView实例初始化,这导致崩溃,因为UIView没有delegate属性。我可以让这个自定义类起作用吗?

Xcode 10.2,iOS 12.2,Swift 4.2

1 个答案:

答案 0 :(得分:1)

我找到了解决方法。首先,创建一个自定义PHLivePhotoView子类:

import PhotosUI

class LivePhotoView: PHLivePhotoView {
}

然后,将View拖到情节提要中并为其设置自定义类:

Custom Class

别忘了检查Inherit Module From Target