ios - swift 3 : Compiler does not recognize type of subclassed UIView

标签 ios swift uiview swift3

我在构建项目时收到以下错误消息:

MyUIController:无法将类型“(LoadingView).Type”(又名“LoadingView.Type”)的值转换为预期的参数类型“UIView”

如果我的 LoadingView 类是 UIView 的子类,我不知道我是怎么得到这个错误的。

我的用户界面 Controller :

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

加载 View :

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

最佳答案

@nyohu 是对的。您在这里所做的是加载类定义而不是它的一个实例。您还可以使用这样的框架定义新 View

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

关于ios - swift 3 : Compiler does not recognize type of subclassed UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41687391/

相关文章:

ios - 无法从 facebook、ios 获取电子邮件。始终获取 null

ios - 有没有办法以编程方式发送消息,而无需用户点击屏幕确认发送消息,而只是使用语音命令?

ios - 将 UILabel 粘贴到 UIView 或 UIImageView

iphone - UIView 从 superview 中移除的速度很慢

ios - 是否可以在 UITableViewCell 中添加 UITableView

iOS UITableViewController静态tableView不滚动

android - Flutter:错误:找不到 Getter: 'suspending' 。案例 AppLifecycleState.suspending

arrays - Swift 数组 - "Int.random(in: 0...25)"和 "randomElement()"之间的差异

ios - iOS 9 中是否有用于 3d touch 的公共(public) API?

ios - Swift:通过其 super View 窗口中的平移手势更改(翻译)UIView 位置