43

在我的应用程序中,我想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;
}
4

10 回答 10

68
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];

有用!

于 2013-11-15T08:31:17.240 回答
12

Xcode 8 + 斯威夫特

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

根据文档,要接收没有相关坐标值的事件,例如键盘输入,请使其key不仅仅是! isHidden

coveringWindow.makeKeyAndVisible()

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

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

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

于 2016-09-29T16:03:52.040 回答
9

你的window1对象是一个局部变量,当代码用完这个方法时,这个对象就不存在了。我们创建的任何UIWindow对象都将被添加到 中[[UIApplication sharedApplication] windows],但这个数组只保留对任何对象的弱引用UIWindow,因此由您自己的代码来保持窗口对象存在。我猜为什么苹果这样实现它,[UIApplication sharedApplication]只要应用程序运行,对象就存在,这样做是为了避免让UIWindow只需要存在一段时间的对象“永远”存在于内存中。

更重要的是,您的代码可以使用 MRC 运行。

于 2015-01-27T06:16:59.413 回答
6

如果您使用的是窗口场景,请尝试以下操作:

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()
于 2021-04-25T07:37:40.927 回答
4

斯威夫特 5

在 iOS13 及更高版本中,您必须UIWindow使用windowScene:初始化程序创建如下let overlayWindow = UIWindow(windowScene: YourScene)。对于较低的 iOS 版本,您应该使用frame:初始化程序,如下所示let overlayWindow = UIWindow(frame: YourFrame)

创建UIWindow实例后,您可以使用代码显示窗口,如@Nick Greg 回答中所写:

overlayWindow.windowLevel = UIWindow.Level.alert
overlayWindow.rootViewController = UIViewController()//your controller or navController
overlayWindow.makeKeyAndVisible()
于 2021-06-23T08:00:26.480 回答
3

斯威夫特 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()
}
于 2018-09-26T20:57:10.800 回答
1

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

UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: yourViewController)
于 2019-11-10T13:05:37.163 回答
0

尝试UIView在 mainWindow 上添加一个而不是另一个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;
}
于 2013-11-15T07:29:19.587 回答
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()
}
于 2020-09-25T02:45:40.350 回答
-1

在swiftUIWindow中可以添加一个新的,如下所示..

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..
}
于 2015-04-20T09:22:53.000 回答