How to constrain second NSViewController minimum size in OS X app?
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
Changing OSX app window size based on user input that changes views
I am answering my own question here - still unable to bind IBOutlet with NSWindow. Anyone with better solutions, please let me know.
Below is a capture of my Main.storyboard. All of the coding is done in the class junkViewController2 that is associated with the 1st view controller.
Here is the code for junkViewController2. I know, it could be more concise ...
//
// JunkViewController2.swift
// Scratch1
//
import Cocoa
class JunkViewController2: NSViewController {
var junkSplitViewController: JunkSplitViewController!
var myWindow: NSWindow!
var dy: CGFloat!
@IBOutlet weak var mySlider: NSSlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
dy = 0.0
}
@IBAction func mySlider(sender: NSSlider) {
let junkSplitViewController = self.childViewControllers[0]
dy = 0.0
for controller in junkSplitViewController.childViewControllers {
if controller.title == "smallGroup1" {
if mySlider.intValue > 0 {
if controller.view.hidden {
dy = dy + 30.0
}
controller.view.hidden = false
} else {
if !controller.view.hidden {
dy = dy - 30.0
}
controller.view.hidden = true
}
}
if controller.title == "smallGroup2" {
if mySlider.intValue > 1 {
if controller.view.hidden {
dy = dy + 30.0
}
controller.view.hidden = false
} else {
if !controller.view.hidden {
dy = dy - 30.0
}
controller.view.hidden = true
}
}
if controller.title == "smallGroup3" {
if mySlider.intValue > 2 {
if controller.view.hidden {
dy = dy + 30.0
}
controller.view.hidden = false
} else {
if !controller.view.hidden {
dy = dy - 30.0
}
controller.view.hidden = true
}
}
}
resize()
}
func resize() {
var windowFrame = NSApp.mainWindow!.frame
let oldWidth = windowFrame.size.width
let oldHeight = windowFrame.size.height
let old_x = windowFrame.origin.x
let old_y = windowFrame.origin.y
let toAdd = CGFloat(dy)
let newHeight = oldHeight + toAdd
let new_y = old_y - toAdd
windowFrame.size = NSMakeSize(oldWidth, newHeight)
windowFrame.origin = NSMakePoint(old_x, new_y)
NSApp.mainWindow!.setFrame(windowFrame, display: true)
}
}
Simple displaying of second window: OS X & Swift
First make new file like:
After that, put these codes in your classes and that should do it.
class SecondWindowController: NSWindowController {
convenience init() {
self.init(windowNibName: "SecondWindowController")
}
}
class ViewController: NSViewController {
private var secondWindowController: SecondWindowController?
@IBAction func showSecondWindow(sender: AnyObject) {
if secondWindowController == nil {
secondWindowController = SecondWindowController()
}
secondWindowController?.showWindow(self)
}
}
Resizing NSWindow to match view controller size in storyboard
The auto layout answer is half of it. You need to set the preferredContentSize in your ViewController for each tab to the fitting size (if you wanted the tab to size to the smallest size satisfying all constraints).
override func viewWillAppear() {
super.viewWillAppear()
preferredContentSize = view.fittingSize
}
If your constraints are causing an issue below try first with a fixed size, the example below sets this in the tab item's view controller's viewWillAppear function (Swift used here, but the Objective-C version works just as well).
override func viewWillAppear() {
super.viewWillAppear()
preferredContentSize = NSSize(width: 400, height: 280)
}
If that works, fiddle with your constraints to figure out what's going on
Resizing the window according to a variable swift
Let's say your window has an IBOutlet named "window", and your dynamic number is named "myDynamicNumber":
func resize() {
var windowFrame = window.frame
let oldWidth = windowFrame.size.width
let oldHeight = windowFrame.size.height
let toAdd = CGFloat(myDynamicNumber)
let newWidth = oldWidth + toAdd
let newHeight = oldHeight + toAdd
windowFrame.size = NSMakeSize(newWidth, newHeight)
window.setFrame(windowFrame, display: true)
}
Related Topics
Realm + Nstableview + Nsarraycontroller
Can Not Import Tensorflow for Swift in Xcode Playground
Prevent Nscollectionview 'Lifting' an Item During Drag
In App Purchase in Skscene Not Working
Siri Remote's Menu Button Not Exiting Application When UIbutton Is Focused
How to Find The Top 3 Maximum Values in a Swift Dictionary
Verifying The Purchase (Receipt) of Another Application (Mac App Store)
Realitykit Entity Synchronization Is Always Nil
Data Structure for Fast Lookup with Multiple Criteria
Showing Action Sheet in The Custom Cell in Swift
Prefix(_ Maxlength:) Is Type-Erased When Used with a Struct That Conforms to Lazysequenceprotocol
How to Move an Object Towards a Direction Without Stopping
Implement a Custom Staggeregrid in UIview Like Etsy App in Swift
How to Hide The Top Bar (With Buttons) Usin Swift and Macos
Automatic Select a Date in Datepicker in Swift Language
Uisearchcontroller Hiding Status Bar on iOS 11 Swift 4