user1898829
user1898829

Reputation: 3517

How to I access a view in a view controllers extension

I'm getting an error for self.view saying 'UIViewController.Type' does not have a member named '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)
    }

Upvotes: 1

Views: 87

Answers (1)

Ian MacDonald
Ian MacDonald

Reputation: 14020

You have defined addBackgroundImage as a class func. This means self will be the class object, not an instance of the class.

Use func addBackgroundImage() { ... } instead.

Upvotes: 2

Related Questions