将contentViewController设置为NSTabViewController后,NSWindow以错误的大小启动 [英] NSWindow launching with wrong size after setting contentViewController to NSTabViewController

查看:173
本文介绍了将contentViewController设置为NSTabViewController后,NSWindow以错误的大小启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有NSWindowController的Xcode项目,其contentViewController设置为NSViewController的子类.我最近从情节提要中删除了NSViewController子类,并用NSTabViewController子类替换了contentViewController.

I have an Xcode project with an NSWindowController whose contentViewController was set to a subclass of NSViewController. I recently removed the NSViewController subclass from the storyboard and replaced the contentViewController with an NSTabViewController subclass.

现在,当我运行应用程序时,NSWindow会以500x500的大小打开,而不是第一个选项卡的大小.而且,在故事板中可以看到 no 视图,该视图的尺寸为500x500,并且该尺寸也不是通过编程方式实现的.窗口本身被设置为不同的大小,如NSTabViewController的第一个NSViewController中的视图一样.

Now, when I run the application, the NSWindow opens with a size of 500x500 instead of the size of the first tab. What's more, there is no view I can see in the storyboard that has a size of 500x500, and that size isn't being programmatically, either. The window itself is set to a different size, as is the view in the NSTabViewController's first NSViewController.

我假设必须在某处设置某种约束,但是如果存在,我不知道在哪里/如何找到它.使用Xcode 9.2和High Sierra.

I'm assuming that there is some sort of constraint I have to set somewhere, but if there is, I don't know where/how to find it. Using Xcode 9.2 and High Sierra.

windowDidLoad()中以编程方式将窗口的大小设置为正确的大小是可行的,但是,如果我更改了视图的大小,则也必须更改它,这会很快变旧.

Programmatically setting the window's size to the correct size in windowDidLoad() works, but if I ever change the size of the view, I'll have to change that, as well, which will get old, quick.

很抱歉,如果含糊不清;我真的不知道什么样的屏幕截图或代码片段会有所帮助.

Sorry if this is vague; I genuinely have no clue what kind of screenshot or code snippet would be helpful.

推荐答案

我最近也遇到了这个令人沮丧的问题.

I recently ran into this frustrating problem as well.

有两种方法可以解决此问题:

There are a couple options to workaround this problem:

  1. 如前所述,在每个自定义视图控制器中将preferredContentSize设置为将标签的内容保持在所需的大小.这是不灵活的,但确实有效.

  1. As you mentioned, set preferredContentSize in each of your custom view controllers that hold the tab's content to your desired size. This is inflexible but it does work.

// Swift
class FooViewController: ViewController {

    override func viewWillAppear() {
        super.viewWillAppear()

        preferredContentSize = NSSize(width: 400, height: 280)
    }
}

  • 我在

  • I found a hint to a better solution in this SO answer. You can add a subview (stackview, nsview, etc...) to the main view of the view controller that handles the tab's content (phew!) and then add constraints that pin it to each edge and add constraints that set the size.

    这是屏幕截图,它是Interface Builder中的外观.我添加了堆栈视图,然后添加了6个约束.

    Here's a screenshot of what it looks like in Interface Builder. I added a Stack View and then added 6 constraints.

    希望这会有所帮助.

    这篇关于将contentViewController设置为NSTabViewController后,NSWindow以错误的大小启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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