1
votes

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.

1
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
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

1 Answers

5
votes

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