1

I am a novice Mac OS X developer. I assume this is an easy question, but I haven't been able to find any useful results via searches.

How do I constrain the size of a SECOND view controller?

I started with a simple Mac OS X app, with a single View Controller. I can select the window that contains the View Controller, then select the "Size Inspector" and check the "Minimum Content Size" box, and specify a minimum x and y for the window.

This allows me to specify the minimum size for this first view controller, as I expect. All is good.

Then I add a second view controller, with a Modal segue from the first view controller, triggered by a button press. I add a NSTextView to this second view controller, to display an attributed string. The text view works fine, displaying the attributed string correctly. This text view is a separate window, and has no minimum size constraint.

So how do i specify the minimum size for this second view controller view? Is this typically done in Interface Builder, or programmatically? When I step through the view hierarchy using Document Outline, I don't see how I can specify minimum size using the Size Inspector. Am I missing something??

Here is my simplified code:

file "ViewController.swift"

class ViewController: NSViewController {
...
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    let secondVC: SecondViewController = segue.destinationController as! SecondViewController
    secondVC.reportAttrString = myReport.reportText
    }
...
}

file "SecondViewController.swift"

class SecondViewController: NSViewController {
    var reportAttrString = NSMutableAttributedString()

    @IBOutlet var ReportTextView: NSTextView!
}

I would appreciate any suggestions, or pointers to any documentation or tutorials that may help me.

SDGary
  • 434
  • 3
  • 17
  • By saying "This text view is a separate window", are you saying that you want your second `NSWindow` to have the same minimum content size as the first `NSWindow`? – Michael Dautermann Oct 22 '18 at 01:44
  • I'd prefer to be able to set the minimum x and y for the new window, either programmatically, or through interface builder. I used "separate window" to describe the OS X "modal" behavior of opening a separate window, vs. iOS, which just uses the same window. – SDGary Oct 22 '18 at 16:32

1 Answers1

5

The simplest way to do this would be:

class SecondViewController: NSViewController, NSWindowDelegate {

override func viewWillAppear() {
    super.viewWillAppear()
    self.view.window?.delegate = self
    self.view.window?.minSize = NSSize(width: 100, height: 100)
    }

}

override func viewDidAppear() {
    super.viewDidAppear()
    var frame = self.view.window!.frame
    var initialSize = NSSize(width: 100, height: 100)
    frame.size = initialSize
    self.view.window?.setFrame(frame, display: true)
}

Although if you were looking for a manual approach then the following would work aswell.

class SecondViewController: NSViewController, NSWindowDelegate {

override func viewWillAppear() {
    super.viewWillAppear()
    self.view.window?.delegate = self
    // Set the initial size
    var frame = self.view.window?.frame
    var initialSize = NSSize(width: 100, height: 100)
    frame.size = initialSize
    self.view.window?.setFrame(frame, display: true)
}


func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
    let minimumSize = NSSize(width: 100, height: 100)
    var newSize = NSSize()
    if(frameSize.width < minimumSize.width) {
        newSize.width = minimumSize.width
    } else {
        newSize.width = frameSize.width
    }
    if(frameSize.height < minimumSize.height) {
        newSize.height = minimumSize.height
    } else {
        newSize.height = frameSize.height
    }
    return newSize
}


}

Further reading:

Resize windows event

NSWindow minSize

Resizing the window

HudsonGraeme
  • 399
  • 3
  • 10
  • This is close to what I need. The windowWillResize() method correctly limits the minimum window size, once I try to resize the window. Initially, though, the window is still drawn smaller than the limits I set (I force higher minimum size). I tried viewWillAppear() with and without the NSSize() call. What is the correct way/method to force the initial window size? – SDGary Oct 22 '18 at 16:20
  • @SDGary Updated. – HudsonGraeme Oct 22 '18 at 16:54
  • Outstanding! 99% there. I had to force unwrap window in the line "var frame = self.view.window!.frame". Is this acceptable vs "guard let window = self.view.window else { return } var frame = window.frame" . Where did you get this example code? How did they unwrap/guard the optional? Thanks again. – SDGary Oct 22 '18 at 18:19
  • @SDGary Updated once again, in my experience I believe force-unwrapping is the only way to go as long as it’s done at or after `ViewDidAppear()` is called. – HudsonGraeme Oct 23 '18 at 14:14
  • 1
    I still get a compile error for this code. To fix, I had to force unwrap this line: "var frame = self.view.window!.frame". That fixes the issue, and runs fine. Also, the force unwrap in setFrame() is not needed. I changed this to "self.view.window?.setFrame(frame, display: true)". Also, are you saying that by moving some code to viewDidAppear() that the window has already been created, so forced unwrapping is safe? Thanks again for your time and help! – SDGary Oct 23 '18 at 16:09
  • Whoops, fixed those errors, apologies. Yes, since the window exists for sure when `viewDidAppear` is called. – HudsonGraeme Oct 23 '18 at 16:55