重写window的center方法,在[[NSApplication sharedApplication] runModalForWindow:];之前,把window的minX和minY赋值。
https://developer.apple.com/documentation/appkit/nsapplication/1428436-runmodalforwindow?changes=_2&language=objc
文档里面说的:The window to be displayed modally. If it is not already visible, the window is centered on the screen using the value in its center
method and made visible and key. If it is already visible, it is simply made key.
@interface AlertWindow : NSWindow
@property (nonatomic, assign) float minX;
@property (nonatomic, assign) float minY;
@end
#import "AlertWindow.h"
@implementation AlertWindow
- (void)center {
if (self.minX && self.minY) {
[self setFrameOrigin:NSMakePoint(self.minX, self.minY)];
} else {
[super center];
}
}
网友评论