我收到self.view错误,说“UIViewController.Type”没有名为“view”的成员

extension UIViewController {

    class func addBackgroundImage() {
        let backgroundImageView = UIImageView();
        let backgroundImage = UIImage(named: "splashBackground");
        backgroundImageView.image = backgroundImage;
        self.view.addSubview(backgroundImageView);
        let insets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        backgroundImageView.autoPinEdgesToSuperviewEdgesWithInsets(insets)
    }

最佳答案

您已将addBackgroundImage定义为class func。这意味着self将是类对象,而不是类的实例。

请改用func addBackgroundImage() { ... }

关于ios - 如何在 View Controller 扩展中访问 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26654087/

10-10 23:25