The Dude
The Dude

Reputation: 41

How to fix the position of the window in Mac catalyst?

Every time i open my project it opens in a random position in window. I want to fix the position for example it’s frame (x,y,width,height ) is this possible ?

Upvotes: 1

Views: 1421

Answers (2)

polymerchm
polymerchm

Reputation: 228

I have a file called utilities.swift that this lives in:

    func delay(_ delay: Double, closure: @escaping () -> ()) {  
        let when = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
    }

Then in the scene delegate that the window is associated with:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let sceneWindow = (scene as? UIWindowScene) else { return }
        #if targetEnvironment(macCatalyst)
        sceneWindow.sizeRestrictions?.minimumSize = CGSize(width: 1570, height: 1000) // optimal minumum
        window = Window(frame: sceneWindow.coordinateSpace.bounds)
        #else
        window = UIWindow(frame: sceneWindow.coordinateSpace.bounds)
        #endif
        window?.windowScene = sceneWindow
        window?.windowScene?.delegate = self
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
        let app = UIApplication.shared.delegate as! AppDelegate

        app.doApp(window: window!)
        
        #if targetEnvironment(macCatalyst)
        delay(0) {
            let ns = self.window?.nsWindow
            Dynamic(ns!).setFrame(CGRect(200,200,1200,800), display: true)
            let frame = Dynamic(ns!).frame.asCGRect!
            let size = frame.size
            Dynamic(ns!).setAspectRatio(CGSize(1.0, size.height/size.width))
        }
        #endif

Window is just a subclass of UIWIndow with some app specific variables. Same for ViewCOntroller.

Upvotes: 0

polymerchm
polymerchm

Reputation: 228

You should look at Mhd Hajezi's Dynamic library.

You first need an extension for UIWindow:

    extension UIWindow {
        var nsWindow: NSObject? {
       Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self)
    }

Put the following in your scene delegate (shown here), or somewhere where you can get at the window once its been instantiated (for instance someView.window!)

    let ns = self.window?.nsWindow
    Dynamic(ns!).setFrame(CGRect(250,200,1200,800), display: true)
    let frame = Dynamic(ns!).frame.asCGRect!
    let size = frame.size
    Dynamic(ns!).setAspectRatio(CGSize(1.0, size.height/size.width))

The last two lines force the aspect ratio to remain constant when resizing the window. All the methods exposed are the objective-C methods. Note the position is for the lower left corner. Check out the NSWindow documentation on sizing windows and content.

Upvotes: 1

Related Questions