已设置NSWindowStyleMaskResizable,如何为简易MacOS窗口启用调整大小手柄?
Hey there, the issue you're seeing comes down to two small gaps in your minimal implementation: your manual event loop isn't triggering AppKit's internal layout and tracking area updates, and the default content view might not have the right autoresizing settings to signal the window is resizable. Here are two minimal code fixes to get those resize handles showing up:
Option 1: Use AppKit's Standard Run Loop (Recommended, More Idiomatic)
This is the cleanest approach that aligns with how MacOS apps are supposed to work. Just replace your manual while loop with AppKit's built-in run method, and tweak the delegate to terminate the app properly:
Updated main function:
int main(int argc, const char * argv[]) { SimpleMainWindowDelegate *mainWindowDelegate = [[SimpleMainWindowDelegate alloc] init]; NSRect screenRect = [[NSScreen mainScreen] frame]; NSRect initialFrame = NSMakeRect((screenRect.size.width - GlobalRenderWidth) * 0.5, (screenRect.size.height - GlobalRenderHeight) * 0.5, GlobalRenderWidth, GlobalRenderHeight); NSWindow *window = [[NSWindow alloc] initWithContentRect: initialFrame styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO]; [window setBackgroundColor: NSColor.redColor]; [window setTitle:@"simple-mac-window"]; [window makeKeyAndOrderFront: nil]; [window setDelegate: mainWindowDelegate]; // Swap manual loop for AppKit's standard run loop [NSApp run]; printf("Finished running simple-mac-window."); return 0; }
Then update your window delegate's windowWillClose method to tell the app to exit:
@implementation SimpleMainWindowDelegate - (void)windowWillClose:(id)sender { // Notify AppKit to terminate the application [NSApp terminate:nil]; } @end
With this change, AppKit handles all the event processing, layout updates, and resize handle rendering automatically—exactly what the system expects.
Option 2: Keep Manual Loop, Add Critical Updates
If you want to stick with your manual event loop, you need two small additions:
- Set the content view's autoresizing mask to confirm it can scale with the window:
// Add this right after initializing the window [window.contentView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
- Call
updateWindowsin your loop to trigger AppKit's internal state updates (including the resize handle tracking areas):
while(Running) { NSEvent* event; do { event = [NSApp nextEventMatchingMask: NSEventMaskAny untilDate: nil inMode: NSDefaultRunLoopMode dequeue: YES]; switch([event type]) { default: [NSApp sendEvent: event]; } } while (event != nil); // Add this line to update window state [NSApp updateWindows]; }
Why This Happened
You correctly set the NSWindowStyleMaskResizable flag, but AppKit needs two more things to show resize handles:
- The window's content view must have autoresizing enabled so the system knows the window's content can scale.
- The app must run through AppKit's standard run loop (or explicitly trigger updates) to handle background tasks like setting up the interactive resize hotspots. Your manual loop skipped these essential internal steps, which is why the handles didn't appear.
内容的提问来源于stack exchange,提问作者JimmyDeemo

