xcode 如何在 Objective-C + Cocoa 中创建自定义的无边框 NSWindow?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5013695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a custom borderless NSWindow in Objective-C + Cocoa?
提问by specik
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:
首先让我说这是我的第一个真正的 Cocoa 应用程序。这是一个简单的应用程序,几乎可以在无边框窗口中显示我的网站。我目前创建无边框窗口的方式是使用以下方法:
- (void) awakeFromNib {
[window setStyleMask:NSBorderlessWindowMask];
[window setAcceptsMouseMovedEvents:YES];
[window setMovableByWindowBackground:YES];
[window setLevel:NSNormalWindowLevel];
}
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.
这样做的问题是,结果,窗口中的 WebView 不会将鼠标悬停事件传递给加载页面上的元素,也不会提供在文本字段中键入的功能。我知道我应该创建一个自定义窗口并将 contentView 移动到其中,但我对 Objective-C 太陌生了,无法弄清楚如何做。
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.
此外,我希望能够通过单击并将其拖动到任何地方来移动窗口,但这是一个侧面的想法。我在网上广泛搜索,找不到解决方案。
Contents of my .h file (just in case):
我的 .h 文件的内容(以防万一):
@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
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.
任何帮助将不胜感激,就像我说的,我是 Objective-C 和 Cocoa 的超级新手,但我确实来自 PHP 开发背景。
回答by
As explained in this answer, windows without title or resize bar (including borderless windows) cannot become key windows.
如本答案所述,没有标题或调整大小栏的窗口(包括无边框窗口)不能成为关键窗口。
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.
你是对的覆盖-canBecomeKeyWindow
,但你错过了正确的地方。你不应该在你的应用程序委托中这样做。您需要创建一个NSWindow
子类,然后覆盖该方法。
回答by Antwan van Houdt
This sample code of apple should give you the information you need, its really easy to change the way it works and change it into your own drawn NSWindow ( without a border :D )
苹果的这个示例代码应该给你你需要的信息,它真的很容易改变它的工作方式并将其更改为你自己绘制的 NSWindow (没有边框:D)
http://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html
http://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html