根据变量 swift 调整 window 的大小

Resizing the window according to a variable swift

我有一个 NSViewController 和一个变量 num。我想根据该变量动态更改 window 的大小。在 swift 中有什么方法可以做到这一点吗?

假设您的 window 有一个名为 "window" 的 IBOutlet,您的动态号码名为 "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)
}

Swift 3 中调整大小 window 你使用 setFrame.

来自 ViewController 的示例:

func resizeWin(size:(CGFloat,CGFloat)){

    self.view.window?.setFrame(NSRect(x:0,y:0,width:size.0,height:size.1), display: true)

}

我需要切换查看文本视图,所以我覆盖了 window 一个不可见的视图 - hideRect 就在文本视图的下方;通过这种方式,我可以调整到较小的 (hideRect) 并稍后恢复到原始大小 - origRect。隐藏和在 viewDidLoad() 捕获的原始矩形。 Swift3/Xcode8.3.3

// class global contants
let kTitleUtility =     16
let kTitleNormal =      22

@IBOutlet var hideView: NSView!
var hideRect: NSRect?
var origRect: NSRect?

@IBAction func toggleContent(_ sender: Any) {
    // Toggle content visibility
    if let window = self.view.window {
        let oldSize = window.contentView?.bounds.size
        var frame = window.frame
        if toggleButton.state == NSOffState {

            frame.origin.y += ((oldSize?.height)! - (hideRect?.size.height)!)
            window.setFrameOrigin(frame.origin)
            window.setContentSize((hideRect?.size)!)

            window.showsResizeIndicator = false
            window.minSize = NSMakeSize((hideRect?.size.width)!,(hideRect?.size.height)!+CGFloat(kTitleNormal))
            creditScroll.isHidden = true
        }
        else
        {
            let hugeSize = NSMakeSize(CGFloat(Float.greatestFiniteMagnitude), CGFloat(Float.greatestFiniteMagnitude))

            frame.origin.y += ((oldSize?.height)! - (origRect?.size.height)!)
            window.setFrameOrigin(frame.origin)
            window.setContentSize((origRect?.size)!)

            window.showsResizeIndicator = true
            window.minSize = NSMakeSize((origRect?.size.width)!,(origRect?.size.height)!+CGFloat(kTitleNormal))
            window.maxSize = hugeSize
            creditScroll.isHidden = false
        }
    }
}

这也保留了寡妇的视觉来源和最小尺码。