To create a new UIWindow over the main window

后端 未结 8 2359

In my app I want to create a new UIWindow over the main UIWindow, And I wrote as following, but it don\'t works. first, i create a UIWindow as the

相关标签:
8条回答
  • 2020-12-07 22:59
    func createAdsWindow(){
        let frame = CGRect.init(0, UIScreen.main.bounds.height - 60, UIScreen.main.bounds.width, 60)
        adsWindow = UIWindow.init(frame: frame)
        adsWindow!.backgroundColor = UIColor(colorBackground)
        let adsViewController = UIViewController.init()
        adsViewController.view.backgroundColor = UIColor.red
        adsWindow?.rootViewController = adsViewController
        adsWindow?.windowLevel = UIWindow.Level(rawValue: 2)
        adsWindow?.makeKeyAndVisible()
    }
    
    0 讨论(0)
  • 2020-12-07 23:00
    UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
    window1.backgroundColor = [UIColor redColor];
    window1.windowLevel = UIWindowLevelAlert;
    [window1 makeKeyAndVisible];
    

    Finally I know why it doesn't work, because window1 is a method var, and it will lost after the method executed. So I declare a new @property for it, as

    @property (strong, nonatomic) UIWindow *window2;
    

    and change the code like

    UIWindow *window2 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 80, 320, 320)];
    window2.backgroundColor = [UIColor redColor];
    window2.windowLevel = UIWindowLevelAlert;
    self.window2 = window2;
    [window2 makeKeyAndVisible];
    

    it works!

    0 讨论(0)
  • 2020-12-07 23:02

    Swift 4

    To avoid memory leak, I prefer to initialise my custom window in this way, as proposed by Apple :

    If you want to provide a custom window for your app, you must implement the getter method of this property and use it to create and return your custom window.

    Example:

    var myCustomWindow: UIWindow? = CustomWindow(frame: UIScreen.main.bounds)
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        let mainController: MainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! MainViewController
        self.myCustomWindow?.rootViewController = mainController
        self.myCustomWindow?.makeKeyAndVisible()
    }
    
    0 讨论(0)
  • 2020-12-07 23:11

    Xcode 8 + Swift

    class ViewController: UIViewController {
        var coveringWindow: UIWindow?
        
        func coverEverything() {
            coveringWindow = UIWindow(frame: (view.window?.frame)!)
            
            if let coveringWindow = coveringWindow {
                coveringWindow.windowLevel = UIWindowLevelAlert + 1
                coveringWindow.isHidden = false
            }
        }
    }
    

    According to the documentation, to receive events that do not have a relevant coordinate value, such as keyboard entry, make it key instead of merely ! isHidden:

    coveringWindow.makeKeyAndVisible()
    

    You can even control the transparency of its background, for a smoke effect:

    coveringWindow.backgroundColor = UIColor(white: 0, alpha: 0.5)
    

    Note that such window needs to handle orientation changes.

    0 讨论(0)
  • 2020-12-07 23:17

    try adding a UIView on mainWindow not another UIWindow like...

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor redColor];
        ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = vc;
        [self.window makeKeyAndVisible];
        UIView * viewAlert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
        viewAlert.backgroundColor = [UIColor redColor];
        [self.window.rootViewController.view addSubView:viewAlert];
        /* or you can use following..
        [self.window addSubView:viewAlert];
        */
        [viewAlert release]; //FOR NON ARC
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-07 23:23

    In swift a new UIWindow can be added as follows..

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
        var viewController: ViewController?
        var navigationController: UINavigationController?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.viewController = ViewController(nibName: "ViewController", bundle:NSBundle.mainBundle())
            self.navigationController = UINavigationController(rootViewController: self.viewController!)
            self.window!.rootViewController = self.navigationController
            //  self.window!.addSubview(self.viewController!.view)
            self.window!.makeKeyAndVisible()
            return true
        }
    
        //Other methods..
    }
    
    0 讨论(0)
提交回复
热议问题