Minimize / miniaturize cocoa NSWindow without titlebar

倖福魔咒の 提交于 2019-12-07 12:44:20

问题


I'm stuck! Obviously... because I'm posting a question here.

I have built my own custom window controls for my OS X / cocoa app. The close button works great -- no problems. The minimize button doesn't work at all when I disable the titlebar.

So when the title bar is on like the image above and I hit this method, the minimizing works fine:

ViewController.h

@interface ViewController : NSViewController {

    - (IBAction)minimize:(id)sender;        
    @property (strong) IBOutlet NSButton *btn_minimize;

}
@end

ViewController.m

@implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
    }

    - (IBAction)minimize:(id)sender {
      [[NSApp mainWindow] performMiniaturize:self];
    }

    -(IBAction)terminate:(id)sender {
        [NSApp terminate:self];
    }
@end

Then if I disable the title bar, that same method stops working. No errors, nothing. I've tried both: [[NSApp mainWindow] miniaturize:nil]; and also [[NSApp mainWindow] performMiniaturize:self];. Neither of which worked. Actually... the both work IF the title bar is on. But once I turn it off, neither work.

Thoughts / comments?

Oh, I'm using Storyboards, Xcode 7, and am targeting 10.10 and using the 10.11 SDK if that matters at all.

Thanks in advance.


回答1:


You have to keep the original "traffic light" buttons around and hide them manually.

Here's how I've configured the window:

self.titleVisibility = NSWindowTitleHidden;
self.titlebarAppearsTransparent = YES;
self.movableByWindowBackground = YES;

And here's how I've hidden the traffic lights:

for (id subview in self.contentView.superview.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) {
        NSView *titlebarView = [subview subviews][0];
        for (id button in titlebarView.subviews) {
            if ([button isKindOfClass:[NSButton class]]) {
                [button setHidden:YES];
            }
        }
    }
}

See sample project here.



来源:https://stackoverflow.com/questions/33045075/minimize-miniaturize-cocoa-nswindow-without-titlebar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!