cocoa——让嵌套的 NSView 和 CALayers 按比例调整大小

发布于 2024-10-11 03:15:22 字数 196 浏览 6 评论 0原文

我的 NSWindow 的 contentView 是 NSView 子类。它还有一些其他 NSView 子类作为子视图。子视图是基于图层的,而这些图层又包含子图层。一些子层具有另外的子子层。

我希望在调整窗口大小时,整个内容也按比例调整大小。正确的设置方法是什么?

谢谢

编辑:我根本不使用 Interface Builder。

My NSWindow's contentView is an NSView subclass. It has some other NSView subclasses as subviews. The subviews are layer-based, and those layers in turn contain sublayers. Some of the sublayers have further sub-sublayers.

I want the whole thing to resize proportionally when the window is resized. What is the right way to set it up so that will happen?

Thanks

EDIT: I am not using Interface Builder at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2024-10-18 03:15:22

以下是我为使 NSView 的内容在调整父窗口大小时按比例缩放所做的工作。首先,在界面生成器中,我将 NSView 添加到窗口,然后在 AppDelegate 中添加对它的引用。我的恰好叫做 scrollView。我从 scrollView 中删除了所有自动调整大小的行为。

然后,在我的 AppDelegate 中添加了以下内容:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // keep the aspect ratio constant so that the content looks good
    [window setContentAspectRatio:NSMakeSize(2, 1)];
    window.delegate = self;
}

- (void)windowDidResize:(NSNotification *)notification {
    // size the scrollView to fill the window, but keep its bounds constant
    NSRect rect = [[window contentView] frame];
    NSRect oldBounds = [scrollView bounds];
    [scrollView setFrame:rect];
    [scrollView setBounds:oldBounds];
}

这也将 AppDelegate 变成了窗口委托。好吧,因为我没有太多逻辑。通过在更改框架时保持边界不变,scrollView 的内容将平滑缩小。

Here's what I've done to get the contents of an NSView to scale proportionally as I resize the parent window. First, in interface builder, I added my NSView to the window, then added a reference to it in my AppDelegate. Mine happens to be called scrollView. I removed all of the auto-sizing behaviour from the scrollView.

Then, in my AppDelegate I added this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // keep the aspect ratio constant so that the content looks good
    [window setContentAspectRatio:NSMakeSize(2, 1)];
    window.delegate = self;
}

- (void)windowDidResize:(NSNotification *)notification {
    // size the scrollView to fill the window, but keep its bounds constant
    NSRect rect = [[window contentView] frame];
    NSRect oldBounds = [scrollView bounds];
    [scrollView setFrame:rect];
    [scrollView setBounds:oldBounds];
}

This turns the AppDelegate into the window delegate too. Fine since I've not got much logic in it. By keeping the bounds constant while changing the frame, the contents of scrollView will be scaled down smoothly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文