在我的应用程序中,我想在主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;
}
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];
有用!
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
:
coveringWindow.makeKeyAndVisible()
您甚至可以控制其背景的透明度,以获得烟雾效果:
coveringWindow.backgroundColor = UIColor(white: 0, alpha: 0.5)
请注意,此类窗口需要处理方向更改。
你的window1
对象是一个局部变量,当代码用完这个方法时,这个对象就不再存在了。我们创建的任何UIWindow
对象都将添加到[[UIApplication sharedApplication] windows]
,但是这个数组只保留一周引用任何UIWindow
对象,所以由你自己的代码来保持窗口对象存在。为什么苹果这样实现,我想,是[UIApplication sharedApplication]
只要应用程序运行,对象就会存在,这样做是为了避免保留UIWindow
对象,这些对象只需要存在一段时间才能永远存在于内存中。
更重要的是,您的代码可以与MRC一起运行。
斯威夫特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()
}
尝试在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;
}
在迅速,新的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..
}