ios 在主窗口上创建一个新的 UIWindow
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19995526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To create a new UIWindow over the main window
提问by bohan
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 main window, and then make it key and visible, and then create a new UIWindow
overlay, but nothing happens.
在我的应用程序中,我想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;
}
回答by bohan
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
终于知道为什么不行了,因为window1是一个方法var,执行完方法就会丢失。所以我为它声明一个新的@property,作为
@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!
有用!
回答by SwiftArchitect
Xcode 8 + Swift
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
}
}
}
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
:
根据文档,要接收没有相关坐标值的事件,例如键盘输入,请使其key
而不是仅仅!
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.
请注意,此类窗口需要处理方向更改。
回答by YuLong Xiao
Your window1
object is a local variable, when the code runs out of this method, this object does not exist any more. Any UIWindow
object we created will be add to the [[UIApplication sharedApplication] windows]
, but this array only keep a week reference to any UIWindow
object, so it's up to your own code to keep the window object exist.Why apple achieved like this, I guess, is [UIApplication sharedApplication]
object exists as long as the app runs, doing so to avoid keeping the UIWindow
objects which only need to be exist for a while living in the memory "forever".
你的window1
对象是一个局部变量,当代码用完这个方法时,这个对象就不再存在了。UIWindow
我们创建的任何对象都将被添加到 中[[UIApplication sharedApplication] windows]
,但是这个数组只保留对任何UIWindow
对象的一周引用,因此保持窗口[UIApplication sharedApplication]
对象存在取决于您自己的代码。在应用程序运行时,这样做是为了避免将UIWindow
只需要存在一段时间的对象“永远”留在内存中。
What's more, your code could run with MRC.
更重要的是,您的代码可以与 MRC 一起运行。
回答by user6618855
Swift 4
斯威夫特 4
To avoid memory leak, I prefer to initialise my custom window in this way, as proposed by Apple :
为了避免内存泄漏,我更喜欢按照 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.
如果您想为您的应用程序提供自定义窗口,则必须实现此属性的 getter 方法并使用它来创建和返回您的自定义窗口。
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()
}
回答by Mohamed Shaban
if you just need to change shared window ViewController in swift 5
如果您只需要在 swift 5 中更改共享窗口 ViewController
UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: yourViewController)
回答by Nirav Gadhiya
try adding a UIView
on mainWindow not another UIWindow
like...
尝试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;
}
回答by Ajith K Jose
In swift a new UIWindow
can be added as follows..
在 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..
}