调整 NSWindow 的大小以匹配故事板中的视图控制器大小 [英] Resizing NSWindow to match view controller size in storyboard

查看:25
本文介绍了调整 NSWindow 的大小以匹配故事板中的视图控制器大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 OSX 10.10 上使用 Xcode 6.1.1.我正在尝试 Mac 应用程序的故事板.我有一个 NSTabViewController 使用新的 NSTabViewControllerTabStyleToolbar tabStyle 并且它被设置为窗口控制器的默认视图控制器.如何根据当前选择的视图控制器调整窗口大小?

I am working on Xcode 6.1.1 on OSX 10.10. I am trying out storyboards for Mac apps. I have a NSTabViewController using the new NSTabViewControllerTabStyleToolbar tabStyle and it is set as the default view controller for the window controller. How do I make my window resize according to the current selected view controller?

是否可以完全在 Interface Builder 中完成?这是我的故事板的样子:

Is it possible to do entirely in Interface Builder? Here is what my storyboard looks like:

推荐答案

自动布局的答案是它的一半.您需要在 ViewController 中将每个选项卡的 preferredContentSize 设置为合适的大小(如果您希望选项卡的大小为满足所有约束的最小大小).

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
}

如果您的约束导致下面的问题,请先尝试使用固定大小,下面的示例在选项卡项的视图控制器的 viewWillAppear 函数中设置它(此处使用 Swift,但 Objective-C 版本也能正常工作).

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

这篇关于调整 NSWindow 的大小以匹配故事板中的视图控制器大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆