macos开发-关闭/最小化/全屏居中处理(仿Mac QQ)

https://juejin.cn/post/6891900387868672013

 

关闭/最小化/全屏居中处理(仿Mac QQ),效果如下

image-20201106143307906

//  FSWindowCtl.m
@interface FSWindowCtl ()<NSWindowDelegate>

@end

@implementation FSWindowCtl

- (void)windowDidLoad {
    [super windowDidLoad];
    [self settingWindowStyle];
}
// 设置window样式
- (void)settingWindowStyle {
    self.window.titlebarAppearsTransparent = YES;
    self.window.titleVisibility = NSWindowTitleHidden;
    self.window.styleMask = NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskFullSizeContentView;
    [self.window setMovableByWindowBackground:YES];
    [self updateTitleBarOfWindow:false];
}

// 修改关闭、最小化、全屏的位置
- (void)updateTitleBarOfWindow:(BOOL)fullScreen {
    CGFloat kTitlebarH = 54.0;
    CGFloat kFullScreenButtonYOrigin = 3.0;
    NSRect windowFrame = self.window.frame;
    NSView *titlebarContainerView = [self.window standardWindowButton:NSWindowCloseButton].superview.superview;
    NSRect titlebarContainerFrame = titlebarContainerView.frame;
    titlebarContainerFrame.origin.y = windowFrame.size.height - kTitlebarH;
    titlebarContainerFrame.size.height = (CGFloat)kTitlebarH;
    titlebarContainerView.frame = titlebarContainerFrame;
    
    CGFloat buttonX = 12.0;
    NSButton *closeBtn = [self.window standardWindowButton:NSWindowCloseButton];
    NSButton *minimizeBtn = [self.window standardWindowButton:NSWindowMiniaturizeButton];
    NSButton *zoomBtn = [self.window standardWindowButton:NSWindowZoomButton];
    
    for (NSButton *buttonView in @[closeBtn, minimizeBtn, zoomBtn]) {
        NSRect buttonFrame = buttonView.frame;
        buttonFrame.origin.y = fullScreen ? kFullScreenButtonYOrigin : round((kTitlebarH - buttonFrame.size.height)/2.0);
        buttonFrame.origin.x = (CGFloat)buttonX;
        buttonX = buttonFrame.size.width + buttonX + 6;
        [buttonView setFrameOrigin:buttonFrame.origin];
    }
}

#pragma mark - NSWindowDelegate
- (void)windowDidEnterFullScreen:(NSNotification *)notification {
    [self updateTitleBarOfWindow:YES];
}

- (void)windowDidExitFullScreen:(NSNotification *)notification {
    [self updateTitleBarOfWindow:NO];
}

- (void)windowDidResize:(NSNotification *)notification {
    [self updateTitleBarOfWindow:NO];
}

@end
复制代码

Github Demo


作者:ForgetSou
链接:https://juejin.cn/post/6891900387868672013
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @   itlover2013  阅读(210)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-10-06 Android 穿山甲广告联盟接入
2020-10-06 Mac OS 下使用 afconvert 命令,处理音频格式转换
2020-10-06 IOS 监听锁屏
点击右上角即可分享
微信分享提示