Listen for window resize event in Swift / Objective-C

前端 未结 4 1373
忘了有多久
忘了有多久 2020-12-30 04:35

Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift? I want to create custom window manager that would move

相关标签:
4条回答
  • 2020-12-30 05:06
    1. create an NSWindowController Class
    2. Open your storyboard and find your window controller then set the Class as custom Class (identity inspect tab)
    3. create an NSWindowDelegate Class with windowWillResize method
    4. set the NSWindowDelegate instance as the window.delegate property of your NSWindowController instance

    When user change window size,the windowWillResize method will be called.

    0 讨论(0)
  • 2020-12-30 05:17

    With Swift 3 the windowDidResize method should look like the following:

    func windowDidResize(_ notification: Notification) {
        // Listen to a window resize event
    
    }
    
    0 讨论(0)
  • 2020-12-30 05:22

    first step,you should add NSWindowDelegate and function windowWillResize,full code like this:

    class MainWindowController: NSWindowController, NSWindowDelegate {
    
        override var windowNibName: String? {
            return "MainWindowController"
        }
    
        override func windowDidLoad() {
            super.windowDidLoad()
        }
    
        func windowDidResize(notification: NSNotification) {
            //listen the event
        }
    
    }
    

    then you should set the delegate property,set MainWindowController.xib include file window's delegate property to File's Owner.

    0 讨论(0)
  • 2020-12-30 05:31

    Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift?

    It is possible to do this, a better way would be to have your custom window manager class implement the NSWindowDelegate protocol and set all window's delegate to your custom window manager. This will give you all the resize and move information you require.

    I'm Haskell, not a Swift developer, so I would be very thanful for any code snippet / example showing how to achieve this effect.

    Implementing protocol methods:

    class CustomWindowManager: NSWindowController, NSWindowDelegate {
    
           // Resize Methods
    
           func windowWillResize(sender: NSWindow,toSize frameSize: NSSize) -> NSSize {
    
                // Your code goes here
           }
    
           func windowDidResize(notification: NSNotification) {
    
               // Your code goes here
           }
    }
    

    You can then just chose which protocol methods you want to implement as all methods in the protocol are optional.

    You will have to be able to identify which window is calling the delegate method and track any windows you move or resize due to reacting to the initial window change. See this Stack Overflow question for help with this.

    For getting a list of all windows currently on screen see this Stack Overflow question.

    Edit


    For getting a list of all windows of your application currently on screen see this Stack Overflow question.

    0 讨论(0)
提交回复
热议问题