'Disabling resizing of NSView

I am showing a view as sheet using presentViewControllerAsSheet API of NSViewController. Is there a way to disable resizing of the view that is presented?

I looked online and there are ways to disable resizing of window but not NSView.

Thanks



Solution 1:[1]

I was able to achieve this by setting preferredContentSize on the view controller in viewDidLoad()

[self setPreferredContentSize:self.view.frame.size];

Solution 2:[2]

@dev answer works for me in Swift 4 (Xcode 9.3)

override func viewDidLoad() {
    super.viewDidLoad()

    preferredContentSize = view.frame.size
}

If using storyboard, make sure the class is assigned into the NSViewController.

Solution 3:[3]

Setting preferredContentSize didn't work for me using Swift 4.0 and XCode 9.2, e.g.:
self.preferredContentSize = NSSize(width: 448, height: 250)

However, I can set parameters like minimum size like this in viewDidAppear():
self.view.window?.contentMinSize = NSSize(width: 448, height: 250)

I imagine you can disable resize by also setting maximum size and showsResizeIndicator. Note that setting these parameters in viewDidLoad() did not work.

Solution 4:[4]

my two cents for swift / OSX / catalina

Suppose You want a list to be allowed to shrink only vertically (fixed width)

override func viewDidAppear() {
    self.setFixedWidth()

}

final private func setFixedWidth(){
    self.view.window?.contentMinSize = NSSize(width: 300, height: 250)
    self.view.window?.contentMaxSize = NSSize(width: 300, height: 1250)
}

When dragging you can only expand vertically (not enlarge it).

Solution 5:[5]

In Objective-C and macOS 12.3.1 I experience beachballs when using the methods described here earlier. Simply use fixed size constraints to prevent the NSView from resizing.

  1. First set the view layout options in IB to 'Inferred (Constraints)'

enter image description here

  1. Set the fixed width/height constraints in IB

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dev
Solution 2 DazChong
Solution 3
Solution 4 ingconti
Solution 5 Guruniverse