在主窗口上创建新的UIWindow

时间:2013-11-15 07:15:54

标签: ios uiwindow

在我的应用程序中,我想在主UIWindow上创建一个新的UIWindow,我编写如下,但它不起作用。 首先,我创建一个UIWindow作为主窗口,然后使其成为键和可见,然后创建一个新的UIWindow叠加层,但没有任何反应。

- (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];
    UIWindow *window1 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
    window1.backgroundColor = [UIColor redColor];
    window1.windowLevel = UIWindowLevelAlert;
    [window1 makeKeyAndVisible];
    return YES;
}

10 个答案:

答案 0 :(得分:53)

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

最后我知道为什么它不起作用,因为window1是一个方法var,它会在方法执行后丢失。所以我为它声明了一个新的@property,如

@property (strong, nonatomic) UIWindow *window2;

和 改变代码,如

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

它有效!

答案 1 :(得分:9)

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
        }
    }
}

根据the documentation,要接收没有相关坐标值的事件(例如键盘输入),请将其设为key,而不是仅! isHidden:< / p>

coveringWindow.makeKeyAndVisible()

您甚至可以控制其背景的透明度,以获得烟雾效果:

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

请注意,此类窗口需要处理方向更改。

答案 2 :(得分:8)

您的window1对象是一个局部变量,当代码用完此方法时,该对象不再存在。我们创建的任何UIWindow对象都将添加到[[UIApplication sharedApplication] windows],但此数组仅保留对任何UIWindow对象的一周引用,因此它可以由您自己的代码保留窗口对象存在。为什么苹果这样实现,我想,只要应用程序运行,[UIApplication sharedApplication]对象就存在,这样做是为了避免保留只需要存在一段时间的UIWindow个对象生活在记忆中&#34;永远&#34;。

还有什么,您的代码可以与MRC一起运行。

答案 3 :(得分:2)

快捷键4

为避免内存泄漏,我更喜欢按照Apple的建议以这种方式初始化自定义窗口:

  

如果要为应用提供自定义窗口,则必须实现此属性的getter方法,并将其用于创建和返回自定义窗口。

示例:

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()
}

答案 4 :(得分:2)

如果你使用的是窗口场景,试试这个:

private var overlayWindow: UIWindow!

if let currentWindowScene = UIApplication.shared.connectedScenes.first as?  UIWindowScene {
        overlayWindow = UIWindow(windowScene: currentWindowScene)
    }
overlayWindow.windowLevel = UIWindow.Level.alert
overlayWindow.rootViewController = UIViewController()//your controller or navController
overlayWindow.makeKeyAndVisible()

答案 5 :(得分:1)

SWIFT 5

在 iOS13 及更高版本中,您必须使用 owner@owner-desktop:~/example$ java -jar samples/HelloFX/Maven/hellofx/target/hellofx-1.0-SNAPSHOT-jar-with-dependencies.jar --module-path ./javafx-jmods-11.0.2/ --add-modules=javafx.base,javafx.graphics,javafx.controls,javafx.fxml Error: JavaFX runtime components are missing, and are required to run this application owner@owner-desktop:~/example$ 初始值设定项创建 UIWindow,如下所示windowScene:。对于较低的 iOS 版本,您应该使用 let overlayWindow = UIWindow(windowScene: YourScene) 初始化程序,如下所示 frame:

创建 let overlayWindow = UIWindow(frame: YourFrame) 实例后,您可以使用代码来显示窗口,如@Nick Greg 的回答:

<块引用>
UIWindow

答案 6 :(得分:0)

尝试在mainWindow上添加UIView而不是另一个UIWindow ...

- (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;
}

答案 7 :(得分:0)

在swift中,可以按如下方式添加新的UIWindow ..

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..
}

答案 8 :(得分:0)

如果您只需要在Swift 5中更改共享窗口ViewController

UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: yourViewController)

答案 9 :(得分:0)

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()
}