无法显示视图

时间:2016-08-26 07:26:35

标签: ios swift

我希望在没有网络连接时显示我的自定义视图。

这样的事情:

enter image description here

我在ViewController

中有一个NavigationControllerAppDelegate中的

我这样写:

var statusView: NetworkStatusView?
...

func showNetworkStatusView() {
        if statusView == nil {
            statusView = NetworkStatusView()
            if let statusView = statusView {
                let screenSize = UIScreen.mainScreen().bounds
                statusView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: 60)
                statusView.hidden = false
                if let rootViewController = window?.rootViewController {
                    rootViewController.view.addSubview(statusView)
                }
            }
        }
    }

在我的ViewController中,我这样称呼:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.showNetworkStatusView()

但它并没有显示我的自定义视图。我也在我的view.addSubView(statusView)中尝试ViewController,但也无法工作。我确保调用该函数,因为我已调试。

这是我的NetworkStatusView

import UIKit

class NetworkStatusView: UIView {

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var statusImageView: UIImageView!
    @IBOutlet weak var statusLabel: UILabel!

    required override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initSubviews()
    }

    func initSubviews() {
        let nib = UINib(nibName: "NetworkStatusView", bundle: nil)
        nib.instantiateWithOwner(self, options: nil)
        contentView.frame = bounds
        addSubview(contentView)
    }

}

0 个答案:

没有答案