如何在视图控制器扩展中访问视图

时间:2014-10-30 13:19:05

标签: ios swift uiviewcontroller

我收到错误的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)
    }

1 个答案:

答案 0 :(得分:2)

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

改为使用func addBackgroundImage() { ... }