在statusBar上方添加一个LoaderOverlayView,但不隐藏它

时间:2019-06-19 10:37:11

标签: swift

尽管存在类似的问题,但我仍无法找到适合自己情况的解决方案。

我正在开发以编程方式执行所有UI的应用程序。在我的AppDelegate中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    setupNavigationBarStyle()

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    window?.rootViewController = UINavigationController(rootViewController: HeadlinesViewController(networkHelper: NetworkHelper.shared))

    return true
}

func setupNavigationBarStyle() {
    UIApplication.shared.statusBarView!.backgroundColor = UIColor.statusBarBackground

    UINavigationBar.appearance().barTintColor = UIColor.navigationBarBackground
    UINavigationBar.appearance().tintColor = UIColor.navigationBarTint

    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.navigationBarText]

    UINavigationBar.appearance().isTranslucent = false

    UITabBar.appearance().barTintColor = UIColor.navigationBarBackground
    UITabBar.appearance().tintColor = UIColor.navigationBarTint
}

当我在HeadlinesViewController中时,我想显示一个LoaderOverlayView,它覆盖包括状态栏在内的整个屏幕,但仍将其显示为覆盖视图。

LoaderOverlayView类如下:

public class LoaderOverlay{

    var overlayView = UIView()
    var activityIndicator = UIActivityIndicatorView()

    var window:UIWindow{
        return UIApplication.shared.keyWindow!
    }

    class var shared: LoaderOverlay {
        struct Static {
            static let instance:LoaderOverlay = LoaderOverlay()
        }
        return Static.instance
    }

    public func showOverlay() {

        //¿¿¿???
        window.windowLevel = UIWindow.Level.statusBar

        overlayView = UIView(frame: UIScreen.main.bounds)
        overlayView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.whiteLarge)
        activityIndicator.center = overlayView.center
        overlayView.addSubview(activityIndicator)
        activityIndicator.startAnimating()

        window.addSubview(overlayView)

    }

    public func hideOverlayView() {
        activityIndicator.stopAnimating()
        overlayView.removeFromSuperview()
        window.windowLevel = UIWindow.Level.normal
    }

}

通过更改windowLevel,将显示视图,但缺少状态栏文本。有什么建议吗?

编辑

问题似乎出在当我在“ setupNavigationBarStyle”函数中设置“ statusBarView”背景色时:

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

1 个答案:

答案 0 :(得分:0)

我终于意识到了

UINavigationBar.appearance().barTintColor = UIColor.navigationBarBackground

是一个给人麻烦的人。删除状态栏后,它的背景颜色仍与NavigationBar相同,并且当我显示覆盖视图时它不会受影响。