2

What is the correct way to detect when a NSView is resized ?. I do not see any resize event available on the view or any delegate for the view.

I have added this hack, where I use the drawRect to detect the change in size, but I'm sure there must be a more correct way to do this.

    CGRect m_resizeRect = CGRect.Empty;
    public override void DrawRect(CGRect dirtyRect)
    {
        base.DrawRect(dirtyRect);
        if (this.InLiveResize) {
            if (m_resizeRect.Size != this.Bounds.Size) {
                m_resizeRect = this.Bounds;
                this.OnResize();
            }
        }
    }
    public override void ViewWillStartLiveResize()
    {
        m_resizeRect = this.Bounds;
        base.ViewWillStartLiveResize();
    }
    public override void ViewDidEndLiveResize()
    {
        m_resizeRect = CGRect.Empty;
        base.ViewDidEndLiveResize();
    }
    protected void OnResize() {
        Console.WriteLine("OnResize " + this.Bounds.ToString() );
    }

4 Answers 4

3

You can subscribe to the resize notifications.

Add observer to default notification center:

NSObject NSWindowDidResizeNotificationObject;
public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    NSWindowDidResizeNotificationObject = NSNotificationCenter.DefaultCenter.AddObserver (new NSString ("NSWindowDidResizeNotification"), ResizeObserver, null);
}

NSNotification Action:

public void ResizeObserver (NSNotification notify)
{
    var r = this.View.Frame;
    Console.WriteLine ("{0}:{1}:{1}", notify.Name, r.Height, r.Width);
}

Remove observer (and release memory):

NSNotificationCenter.DefaultCenter.RemoveObserver (NSWindowDidResizeNotificationObject);

Sample Output:

NSWindowDidResizeNotification:740:740
NSWindowDidResizeNotification:715:715
NSWindowDidResizeNotification:681:681
NSWindowDidResizeNotification:642:642
3

You can override the setFrameSize method and do your own stuff every time the frame is updated.

class MyView: NSView {
    ...
    override func setFrameSize(newSize: NSSize) {
        super.setFrameSize(newSize)
        Swift.print("new size is \(frame)")
    }
    ...
}

The accepted answer only seems to respond to window size changes and not e.g. when the split-bar of a splitview causes the resize.

2

See the NSView postsBoundsChangedNotifications and postFrameChangedNotifications properties. You can set those and register for those notifications.

2

You can use NSViewController.viewDidLayout() overriden method:

class MyViewController: NSViewController {
    ...
    override func viewDidLayout() {
        Swift.print("view has been resize to \(self.view.frame)")
    }
    ...
}
6
  • 1
    NSView doesn't have a viewDidLayout() method?
    – Adrian
    Sep 15, 2016 at 18:36
  • Sorry my mistake it's NSViewController I just fixed it. Sep 17, 2016 at 13:47
  • @Adrian I edited my answer, can you please reconsider your downvote? Oct 4, 2016 at 10:33
  • 1
    This no longer pertains to the question. Respectfully suggest deleting the answer?
    – Adrian
    Oct 12, 2016 at 15:59
  • 2
    @MartinDelille It doesn't work if layout method wasn't called after user updated the frame size. Observing NSViewFrameDidChangeNotification is the recommended way.
    – Hao Xi
    Oct 17, 2016 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.