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

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

作者: ForgetSou | 来源:发表于2020-11-06 14:17 被阅读0次

    关闭/最小化/全屏居中处理(仿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

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/vvstbktx.html