问题描述
我正在开发 Cocoa 应用程序,但遇到了一种情况,我希望两个 NSView 对象重叠.我有一个父 NSView,它包含两个子视图(NSView A 和 NSView B),每个子视图都可以有自己的几个子视图.
I'm working on a Cocoa application, and I've run into a situation where I would like to have two NSView objects overlap. I have a parent NSView which contains two subviews (NSView A and NSView B), each of which can have several subviews of their own.
有没有合适的方法来处理这种重叠?NSView B 总是在 NSView A 的上方",所以我希望 NSView A 的重叠部分被屏蔽.
Is there a proper way to handle this kind of overlap? NSView B would always be "above" NSView A, so I want the overlapped portions of NSView A to be masked out.
推荐答案
如果您的应用程序仅适用于 10.5,请为视图打开图层,它应该可以正常工作.
If your application is 10.5-only, turn on layers for the views and it should just work.
如果您打算支持 10.4 及以下版本,则需要找到一种方法来避免视图重叠,因为重叠的同级视图是未定义的行为.正如视图编程指南所说:
If you're meaning to support 10.4 and below, you'll need to find a way not to have the views overlap, because overlapping sibling views is undefined behavior. As the View Programming Guide says:
出于性能原因,Cocoa 不会强制在兄弟视图之间进行剪辑,也不会保证在兄弟视图重叠时正确的失效和绘制行为.如果你想让一个视图绘制在另一个视图的前面,你应该让前视图成为后视图的子视图(或后代).
我已经看到一些技巧有时可以使它有点工作,但这不是您可以依赖的任何东西.您需要将视图 A 设为视图 B 的子视图,或者创建一个巨大的视图来处理它们的两种职责.
I've seen some hacks that can make it kinda work sometimes, but it's not anything you can rely on. You'll need to either make View A a subview of View B or make one giant view that handles both of their duties.
这篇关于有没有正确的方法来处理重叠的 NSView 兄弟姐妹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!