简体   繁体   中英

SwiftSpinner no longer working

I was looking for a way to programmatically set my initial view controller using Storyboards. I found this solution: Programmatically set the initial view controller using Storyboards

This works just liked I want it to but after using the Swift answer provided in that topic:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var exampleViewController: ExampleViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ExampleController") as! ExampleViewController

self.window?.rootViewController = exampleViewController

self.window?.makeKeyAndVisible()

return true
}

When loading the view for the first time the framework that used to work won't work anymore: SwiftLoader. https://github.com/leoru/SwiftLoader

However when I load the same view for the second time the SwiftLoader plugin does activated.

I load SwiftLoader like so:

SwiftLoader.show(title: "Loading...", animated: true)

I've the feeling that it has to do with the code above creating a UIWindow instead of whatever Storyboard used to (UIView?) do but I've no idea as to how to fix this?

SwiftLoader presents using the application's key window. See this source code :

    var currentWindow : UIWindow = UIApplication.sharedApplication().keyWindow!

    // […snip…]

    loader.coverView = UIView(frame: currentWindow.bounds)
    loader.coverView?.backgroundColor = UIColor.clearColor()

    if (loader.superview == nil) {
        currentWindow.addSubview(loader.coverView!)
        currentWindow.addSubview(loader)
        loader.start()
    } else {
        loader.coverView?.removeFromSuperview()
    }

So your problem must be that another window is the key window when you first activate SwiftLoader.

To check this, inspect UIApplication.sharedApplication().keyWindow and compare it to the UIApplication.sharedApplication().windows array to see what all of the current windows in your app are. The frontmost UIWindow will be the last element of this array.

Non-key windows might be iOS keyboards, alert views, etc.

I fixed a similar problem in the SVProgressHUD library by implementing this code :

    NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
    for (UIWindow *window in frontToBackWindows){
        BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
        BOOL windowIsVisible = !window.hidden && window.alpha > 0;
        BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;

        if (windowOnMainScreen && windowIsVisible && windowLevelNormal) {
            [window addSubview:self.overlayView];
            break;
        }
    }

You might want to update SwiftLoader to use smarter logic like this (ported to Swift), or allow you to manually specify a window.

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