john
john

Reputation: 1075

Swift 3 : Compiler does not recognize type of subclassed UIView

I am getting the following error message when building my project:

MyUIController: Cannot convert value of type '(LoadingView).Type' (aka 'LoadingView.Type') to expected argument type 'UIView'

I dont know how im getting this error if my LoadingView class is a subclass of UIView.

MyUIController:

func loadingToLoading() {
        // getting error message at this line
        view.addSubview(LoadingView)
        loadingView.frame = CGRect(origin: .zero, size: view.frame.size)
    }

LoadingView :

import Foundation
import UIKit

final class LoadingView: UIView {

    private let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)

    override init(frame: CGRect) {

        spinner.startAnimating()

        super.init(frame: frame)

        addSubview(spinner)
        backgroundColor = UIColor.white
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        spinner.center = center
    }
}

Upvotes: 0

Views: 222

Answers (1)

Russell
Russell

Reputation: 5554

@nyohu is right. What you're doing here is loading the class definition rather than an instance of it. You can also define your new view with a frame like this

func loadingToLoading() {
    var loadingView = LoadingView(frame: CGRect(origin: .zero, size: view.frame.size))        
    view.addSubview(loadingView)
}

Upvotes: 2

Related Questions