gpt4 book ai didi

objective-c - 如何强制 NSSplitView 的所有 subview 在启动时可见?

转载 作者:行者123 更新时间:2023-12-03 17:12:49 27 4
gpt4 key购买 nike

我正在完全以编程方式制作用户界面。 (没有IB)我制作了一个NSWindow,并附加了一个NSSplitView。问题是当窗口在程序启动时显示时,分割 View 的第一个 subview 总是折叠起来。

如何在启动时强制显示分割 View 的所有 subview ?

最佳答案

这类问题很难证明原因,因为它需要了解闭源程序的内部知识。

所以原因未知,但是当我在添加 subview 之前将 NSSplitView 的初始大小设置为非零值时,会显示 subview 。

NSSplitView* v  = [[NSSplitView alloc] init];
NSView* v2 = [[NSView alloc] init];
v.frame = NSRectFromCGRect(CGRectMake(0,0,100,100)); // Added this line.
v2.frame = NSRectFromCGRect(CGRectMake(0,0,50,100)); // Added this line.
[v addSubview:v2]; // And then, add subview.

我猜 NSSplitView 根据其当前可用大小有一些内部 subview 布局行为。据我观察,

  • 将 subview 添加到零大小的 NSSplitView 永远无法正常工作。

更新

从 OS X 10.10 开始,Cocoa 引入了一个新类 NSSplitViewController,并且它运行得很好。我强烈推荐使用这个。它完全基于自动布局,因此您需要使用自动布局约束来设置大小。

我写了一个working example project ,这是复制的代码片段。

    func make1() -> NSViewController {
let vc1 = NSViewController()
vc1.view = NSView()
vc1.view.wantsLayer = true
vc1.view.layer!.backgroundColor = NSColor.blueColor().CGColor
return vc1
}
func setup1(vc1:NSViewController) {
/// Layout constraints must be installed after the view is added to a view hierarchy.
split1.view.addConstraint(NSLayoutConstraint(item: vc1.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 20))
split1.view.addConstraint(NSLayoutConstraint(item: vc1.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 300))
}

split1.addSplitViewItem(NSSplitViewItem(viewController: make1()))
split1.addSplitViewItem(NSSplitViewItem(viewController: make1()))
split1.addSplitViewItem(NSSplitViewItem(viewController: make1()))

setup1(split1.splitViewItems[0].viewController)
setup1(split1.splitViewItems[1].viewController)
setup1(split1.splitViewItems[2].viewController)

关于objective-c - 如何强制 NSSplitView 的所有 subview 在启动时可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com