调整弹出窗口的大小

时间:2019-06-06 20:56:12

标签: swift macos nsviewcontroller

我想构建一个模态对话框窗口,其中包含许多NSView,在编译时不知道要显示多少个视图。我已经能够用视图填充窗口,但是有两个问题使我停滞不前。一种是根据要显示的项目数来调整窗口的大小。另一篇文章将解决另一个问题。

我使用情节提要通过“确定”和“取消”按钮创建一个窗口,然后将其添加到窗口的内容中

这是我的代码的重要组成部分:

class ParmViewController: NSViewController {

    static func showParmDialog()
    {
        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        let windowController = storyboard.instantiateController(withIdentifier: "ParmViewController") as! NSWindowController
        let window = windowController.window!
        let viewController = window.contentViewController as! ParmViewController
        viewController.setup()
        let application = NSApplication.shared
        application.runModal(for: window)
        window.close()
    }
    let newHeight:CGFloat = 240        
    func setup() {
        print( view.frame )        // prints (0.0, 0.0, 300.0, 180.0)
        view.frame = NSRect( x: 0, y: 0, width: 300, height: newHeight )
        print( view.frame )        // prints (0.0, 0.0, 300.0, 240.0)
    }
}

上面的代码对显示的窗口大小没有影响。该视图有一个超级视图,比该窗口高22像素-调整其大小也无效。

如何在屏幕上调整窗口大小?

1 个答案:

答案 0 :(得分:0)

如果将代码移至viewDidLoad(),则可以调整大小。