subclassing UIWindow while using storyboards
Asked Answered
A

4

31

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?

My question is how do i implement a 'window' getter method in my app delegate that returns 'MyWindow' subclass? Or maybe there is other ways to assign my subclass to my app's main window?

Aspirator answered 1/5, 2012 at 19:31 Comment(0)
S
83

UIWindow in a Storyboard project can be subclassed as explained in Apple's UIApplicationDelegate reference:

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. You may implement the getter method of this protocol to provide the application with a different window.

In other words in your AppDelegate implementation simply add the following getter

Objective-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 { }
}
Selfpronouncing answered 14/5, 2012 at 8:48 Comment(2)
Can you please provide how I can override getter in Swift?Mazzard
Thanks a lot..It worked.I was not creating setter for that so that I was getting error.Mazzard
Z
3

Its not so hard you're going to first subclass UIWindow

class WinCustom : UIWindow{ 
....
}

then in 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
}
Zayas answered 2/3, 2017 at 22:33 Comment(0)
N
1

In my own apps, I've seen "window" property declared in AppDelegate.h when creating a new app from the Xcode templates.

You can modify that property to change from "UIWindow" to "MyWindow" at that point.

Or, a less elegant solution, you can simply cast window's return to a "MyWindow" object type when accessing it.

Nickelplate answered 1/5, 2012 at 20:16 Comment(4)
Neither of those will cause a MyWindow to get created—they’re just mistakenly casting an already-existing UIWindow to a MyWindow.Endemic
@NoahWitherspoon - vfxdomain probably already now knows how to create a MyWindow (since he referred to this related question in his posting up there). My take on his question was he was wondering how to get MyWindow returned by the getter method of the "window" property.Nickelplate
@Michael Dautermann - yep, that's what i am asking... Changing UIWindow to myWindow in App Delegate won't work as Noah Witherspoon pointed.Aspirator
The second sentence in this answer pretty much says "Customize to your liking" yet you have no upvotes! Can people not infer things?? Dear sir, I will give you an up++Tristichous
D
0

UIApplicationDelegate protocol has window property which you can use

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

[Set UIWindow]

Darfur answered 29/4, 2020 at 15:3 Comment(1)
A protocol does not decide whether a property is computed or not. That's just wrong.Martguerita

© 2022 - 2024 — McMap. All rights reserved.