简体   繁体   English

使用故事板时继承 UIWindow

[英]subclassing UIWindow while using storyboards

I have same issue as explained in this question:我有与这个问题中解释的相同的问题:

Where can I change the window my app uses from UIWindow to my own subclass "MyWindow" with storyboard? 我在哪里可以将我的应用程序使用的窗口从 UIWindow 更改为我自己的带有故事板的子类“MyWindow”?

My question is how do i implement a 'window' getter method in my app delegate that returns 'MyWindow' subclass?我的问题是如何在返回“MyWindow”子类的应用程序委托中实现“window”getter 方法? Or maybe there is other ways to assign my subclass to my app's main window?或者也许还有其他方法可以将我的子类分配给我的应用程序的主窗口?

UIWindow in a Storyboard project can be subclassed as explained in Apple's UIApplicationDelegate reference:可以按照 Apple 的UIApplicationDelegate参考中的说明对 Storyboard 项目中的UIWindow进行子类化:

window窗户
When a storyboard is being used, the application must present the storyboard by adding it to a window and putting that window on-screen.使用故事板时,应用程序必须通过将故事板添加到窗口并将该窗口放在屏幕上来呈现故事板。 The application queries this property for the window.应用程序为窗口查询此属性。 The retained reference to the window by this property is necessary to keep the window from being released.此属性保留对窗口的引用是必要的,以防止窗口被释放。 If the value of the property is nil (the default), the application creates a generic instance of UIWindow and assign it to this property for the delegate to reference.如果该属性的值为nil (默认值),则应用程序会创建一个UIWindow的通用实例并将其分配给该属性以供委托引用。 You may implement the getter method of this protocol to provide the application with a different window.你可以实现这个协议的 getter 方法来为应用程序提供一个不同的窗口。

In other words in your AppDelegate implementation simply add the following getter换句话说,在您的AppDelegate实现中只需添加以下 getter

Objective-C目标-C

- (MyCustomWindow *)window
{    
    static MyCustomWindow *customWindow = nil;
    if (!customWindow) customWindow = [[MyCustomWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    return customWindow;
}

Swift迅速

var customWindow: MyCustomWindow?    
var window: UIWindow? {
    get {
        customWindow = customWindow ?? MyCustomWindow(frame: UIScreen.mainScreen().bounds)
        return customWindow
    }
    set { }
}

Its not so hard you're going to first subclass UIWindow你要先继承 UIWindow 并不难

class WinCustom : UIWindow{ 
....
}

then in AppDelegate:然后在 AppDelegate 中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    self.window = WinCustom(frame: UIScreen.main.bounds)

    self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()

    return true
}

In my own apps, I've seen " window " property declared in AppDelegate.h when creating a new app from the Xcode templates.在我自己的应用程序中,我在从 Xcode 模板创建新应用程序时看到在 AppDelegate.h 中声明了“ window ”属性。

You can modify that property to change from " UIWindow " to " MyWindow " at that point.此时您可以修改该属性以从“ UIWindow ”更改为“ MyWindow ”。

Or, a less elegant solution, you can simply cast window 's return to a " MyWindow " object type when accessing it.或者,一个不太优雅的解决方案,您可以在访问它时简单地将window的返回转换为“ MyWindow ”对象类型。

UIApplicationDelegate protocol has window property which you can use UIApplicationDelegate协议具有您可以使用的window属性

import UIKit

class CustomWindow : UIWindow {
    //...
}
class AppDelegate: UIResponder, UIApplicationDelegate {

    var customWindow: CustomWindow?

    var window: UIWindow? {
        get {
            customWindow = customWindow ?? CustomWindow(frame: UIScreen.main.bounds)
            return customWindow
        }
        set { }
    }

    //...
}

This solution just returns a custom UIWindow此解决方案仅返回一个自定义 UIWindow

[Set UIWindow] [设置UIWindow]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM