如何在Objective-C + Cocoa中创建自定义无边框NSWindow? [英] How do I create a custom borderless NSWindow in Objective-C + Cocoa?

查看:748
本文介绍了如何在Objective-C + Cocoa中创建自定义无边框NSWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先说说这是我第一个真正的Cocoa应用程序。这是一个简单的应用程序,几乎显示我的网站在无边框的窗口。我现在创建一个无边框窗口的方式是使用以下:

Let me start off by saying that this is my first real Cocoa app. It's a simple app that pretty much displays my website in a borderless window. The way I'm currently creating a borderless window is using the following:

- (void) awakeFromNib {
    [window setStyleMask:NSBorderlessWindowMask];
    [window setAcceptsMouseMovedEvents:YES];
    [window setMovableByWindowBackground:YES];
    [window setLevel:NSNormalWindowLevel];
}

这样做的问题是,不会将鼠标悬停在加载的页面上的元素上,也不能在文本字段中输入。我知道我应该创建一个自定义窗口,并移动contentView到它,但我是太新的Objective-C来弄清楚如何。

The problem with this is that as a result, the WebView within the window does not pass mouse over events to elements on the loaded page, nor does it provide the ability to type in text fields. I know that I'm supposed to create a custom window instead and move the contentView into it but I'm too new to Objective-C to figure out how.

我还尝试声明所有这些没有运气:

I've also tried declaring all of these with no luck:

@implementation specikAppDelegate

@synthesize window;
@synthesize webView;

- (BOOL) canBecomeKeyWindow { return YES; }
- (BOOL) canBecomeMainWindow { return YES; }
- (BOOL) acceptsFirstResponder { return YES; }
- (BOOL) becomeFirstResponder { return YES; }
- (BOOL) resignFirstResponder { return YES; }

...

@end

另外,我想通过点击并拖动它在任何地方移动窗口,但这是一个想法。我已经在线搜索,找不到解决方案。

Additionally, I'd like to be able to move the window by clicking and dragging it anywhere but that's a side thought. I've searched extensively online, and cannot find a solution to this.

我的.h文件的内容(以防万一):

Contents of my .h file (just in case):

@interface specikAppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet NSWindow *window;
    IBOutlet WebView *webView;
}

@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet WebView *webView;

- (IBAction)openAboutPanel:(id)sender;

@end

任何帮助将不胜感激,我是Objective-C和Cocoa的世界的新手,但我确实来自PHP开发背景。

Any help would be appreciated, and like I said, I'm super new to the world of Objective-C and Cocoa, but I do come from a PHP development background.

推荐答案

此回答,没有标题或调整大小条的窗口(包括无边框窗口)不能成为关键窗口。

As explained in this answer, windows without title or resize bar (including borderless windows) cannot become key windows.

你是对的重写 -canBecomeKeyWindow ,但你错过了正确的地方。你不应该在你的应用程序委托。您需要创建一个 NSWindow 子类,然后重写该方法。

You were right about overriding -canBecomeKeyWindow, but you’ve missed the correct place. You shouldn’t do it in your application delegate. You need to create an NSWindow subclass and then override that method.

这篇关于如何在Objective-C + Cocoa中创建自定义无边框NSWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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