NSImageView 中的图像和按钮位置错误

发布于 2024-11-13 06:09:19 字数 1211 浏览 4 评论 0原文

我将一个按钮和一个图像分配到同一位置 (0, 0),但它们绘制在不同的位置。为什么会出现这种情况?

这是我的代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{

    // Insert code here to initialize your application 
    [window makeKeyWindow];
    [window setFrame:NSMakeRect(0, 0, 500, 468) display:YES];
    //[window setFrame:[[NSScreen mainScreen]frame] display:YES];
    [window setBackgroundColor:[NSColor clearColor]];
    [window center];
    [[window contentView] setAutoresizesSubviews:YES];

    NSImageView *subView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 500, 468)];
    [subView setImage:[NSImage imageNamed:@"Flowers.jpg"]];
    [subView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
    [subView setAutoresizesSubviews:YES];

    NSButton *subButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, 0, 100, 40)];
    [subButton setTitle:@"testing"];
    [subButton setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable|NSViewMaxXMargin|NSViewMaxYMargin|NSViewMinXMargin|NSViewMinYMargin];
    [subView addSubview:subButton];
    [window setContentView:subView];
}

这是构建后的结果:

Screenshot

I am assigning a button and an image to the same position (0, 0), but they are drawn in different locations. Why does this happen?

Here is my code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{

    // Insert code here to initialize your application 
    [window makeKeyWindow];
    [window setFrame:NSMakeRect(0, 0, 500, 468) display:YES];
    //[window setFrame:[[NSScreen mainScreen]frame] display:YES];
    [window setBackgroundColor:[NSColor clearColor]];
    [window center];
    [[window contentView] setAutoresizesSubviews:YES];

    NSImageView *subView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 500, 468)];
    [subView setImage:[NSImage imageNamed:@"Flowers.jpg"]];
    [subView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
    [subView setAutoresizesSubviews:YES];

    NSButton *subButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, 0, 100, 40)];
    [subButton setTitle:@"testing"];
    [subButton setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable|NSViewMaxXMargin|NSViewMaxYMargin|NSViewMinXMargin|NSViewMinYMargin];
    [subView addSubview:subButton];
    [window setContentView:subView];
}

and here is the result after building:

Screenshot

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

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

发布评论

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

评论(1

奢华的一滴泪 2024-11-20 06:09:19

该代码完全按照您的指示运行;按钮和图像视图位于正确的位置,都位于窗口的左下角。

图像不会填充图像视图的整个高度,因为它具有不同的纵横比:它的宽度明显大于高度,而图像视图几乎是正方形的。

没有办法改变图像视图以不同的方式工作——填充图像视图,即使它必须裁剪图像才能做到这一点。唯一的选择是按比例缩放(保留纵横比)、无论比例如何缩放(填充图像视图但可能扭曲图像)以及永远不缩放。如果您想按比例缩放以填充和裁剪,则必须自己完成。或者,您可能想要更改窗口的大小以匹配图像的大小(或至少是宽高比)。

顺便说一句,在 Cocoa 中将按钮放在图像视图中是不寻常的。大多数视图(执行特定操作的视图)不应包含其他视图。按钮和图像视图都应该位于普通 NSView 内。

The code is working exactly as you told it to; the button and the image view are in the correct position, both in the lower-left corner of the window.

The image does not fill the entire height of the image view because it has a different aspect ratio: It is significantly wider than it is tall, while the image view is nearly square.

There is no way to change the image view to work differently—to fill the image view even if it must crop the image to do so. The only options are to scale proportionally (preserving aspect ratio), scale regardless of ratio (fill the image view but possibly distort the image), and don't scale ever. If you want to scale proportionally to fill and crop, you will have to do it yourself. Alternatively, you may want to change the window's size to match the size—or at least the aspect ratio—of the image.

As an aside, it is unusual at best to put buttons inside image views in Cocoa. Most views—those that do something specific—should not contain other views. The button and the image view should each be inside a plain NSView.

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