5

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];
}

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.

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

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.

specik
  • 113
  • 1
  • 5
  • In which class have you declared/defined `-canBecomeKeyWindow`? –  Feb 16 '11 at 07:35
  • 1
    Bavarious: @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; } – specik Feb 16 '11 at 07:37

2 Answers2

9

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.

Community
  • 1
  • 1
  • Gotcha. I figured I needed to create a subclass, but have not been able to figure it out nor do I know how I'd move the window contentView into it. – specik Feb 16 '11 at 07:50
  • 1
    @specik Create an `NSWindow` subclass in your Xcode project, e.g. `MyWindow`, and override that method. In Interface Builder, set your window class to `MyWindow`: select the window, open the Identity Inspector (menu Tools > Identity Inspector) and type `MyWindow` in Class Identity > Class. –  Feb 16 '11 at 07:54
  • Wow that was super easy. And I learned a lot about how classes behave. Thanks a lot for that! – specik Feb 16 '11 at 08:07
3

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 )

http://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52