简体   繁体   中英

Insert UIView at top of other views programmatically

I work on iOS app, and I need a uiview appear in each uiviewcontroller when there is no internet connection available as in facebook messenger, I write the following code to do that:

 extension UIViewController: NetworkStatusListener {

        public func networkStatusDidChange(status: Reachability.NetworkStatus) {

            switch status {
            case .notReachable:
                debugPrint("ViewController: Network became unreachable")
            case .reachableViaWiFi:
                debugPrint("ViewController: Network reachable through WiFi")
            case .reachableViaWWAN:
                debugPrint("ViewController: Network reachable through Cellular Data")
            }

            let headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 30))
            let headerLbl = UILabel()
            headerLbl.frame = CGRect(x: 0, y: 0, width:  self.view.frame.size.width, height: 30)
            headerLbl.text = "No Internet Connection!"
            headerLbl.backgroundColor = UIColor.red
            headerLbl.font = UIFont(name: "Cairo", size: 14)
            headerLbl.textAlignment = .center
            headerView.addSubview(headerLbl)

           //
            self.view.insertSubview(headerView, at: 0)

            headerView.isHidden = (status == .notReachable)
        }
      override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        ReachabilityManager.shared.addListener(listener: self)
    }

      override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        ReachabilityManager.shared.removeListener(listener: self)
    }

}

I depend in checking network availability on this
The checking of network is worked correctly but the desired target isn't achieved. I need no network connection appear in each uiviewcontroller of my project when there is no internet connection and at top of all other views without overlap other or above them, how can I do that?

I'd prefer a message extension like SwiftMessages . You can easily call the function from any class/function eg your network-availability-function.

in your case it'd look like this:

public func networkStatusDidChange(status: Reachability.NetworkStatus) {

        switch status {
        case .notReachable:
            debugPrint("ViewController: Network became unreachable")
            let view = MessageView.viewFromNib(layout: .cardView)
            view.configureTheme(.warning)
            view.configureDropShadow()
            view.configureContent(title: "Warning", body: "Network became unreachable.")
            SwiftMessages.show(view: view)
        case .reachableViaWiFi:
            debugPrint("ViewController: Network reachable through WiFi")
        case .reachableViaWWAN:
            debugPrint("ViewController: Network reachable through Cellular Data")
        }
    }

For tableviews you can use some Pods like this: https://github.com/HamzaGhazouani/HGPlaceholders or https://github.com/dzenbot/DZNEmptyDataSet

a couple of things...

First, in your networkStatusDidChange method, you are creating a new headerView each time - regardless of the network availability status. You might want to a) only create the headerView when you actually want to show it; and b) only create it if you haven't already.

Second, I'm pretty sure inserting the view at index 0 puts it at the bottom. Instead, you could just addSubview the first time around, and then call bringSubview(toFront:) on it when you want to show it again.

If your requirement for having each VC be able to display a status message isn't too strict, you may want to consider an alternate, and the reason I bring this up is that I'm not sure how well this approach plays with creating other views / view controllers - ie what happens to the status message when a new view is pushed onto a navigation controller, or the user selects another tab in your app (I have no idea about your app, so I'm just providing examples).

If it makes sense for your app, you could also create a dedicated UIViewController subclass for showing the status (instead of an extension that any view controller can access). Then, set up a view controller hierarchy in your app - for example, if your app is a tab bar app:

AppRootViewController
    YourTabBarController
    YourNetworkStatusViewController

The net status vc can add/remove its view as needed, in response to network availability changes. The advantage is a separation of concerns - only that one VC knows about showing the network status, and all your other view controllers remain independent from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM