目前,我要停止调整窗口大小:
#if targetEnvironment(macCatalyst)
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 480, height: 900)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 480, height: 900)
#endif
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
但全屏按钮仍然使其全屏显示。
这有点复杂,但可能。这是一种方法(我删除了所有目标宏以简化发布)。
结果:
码:
// on next event after UIWindow has made key it is possible to find NSWindow in runtime
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
...
window.makeKeyAndVisible()
DispatchQueue.main.async { // < wait for NSWindow available
SilentBridge.disableCloseButton(for: self.nsWindow(from: window))
}
}
// added helper function to SceneDelegate to find NSWindow
func nsWindow(from window: UIWindow) -> NSObject? {
guard let nsWindows = NSClassFromString("NSApplication")?.value(forKeyPath: "sharedApplication.windows") as? [NSObject] else { return nil }
for nsWindow in nsWindows {
let uiWindows = nsWindow.value(forKeyPath: "uiWindows") as? [UIWindow] ?? []
if uiWindows.contains(window) {
return nsWindow
}
}
return nil
}
首选Objective-C部分(使用未声明的选择器更简单)。通过Xcode模板添加新的Objective-C类,并确认创建网桥。之后,需要在生成的类头文件下面添加它,*-Bridging-Header.h
并且所有文件都应该工作。
// SilentBridge.h
@import Foundation;
@interface SilentBridge : NSObject
+ (void)disableCloseButtonFor:(NSObject * _Nullable)window;
@end
// SilentBridge.m
#import "SilentBridge.h"
@import Foundation;
// Forward declarations to allow direct calls in below method
@interface NSObject(SilentBridge)
- (id)standardWindowButton:(NSInteger)value;
- (void)setEnabled:(BOOL)flag;
@end
@implementation SilentBridge
+ (void)disableCloseButtonFor:(NSObject *)window {
if ([window respondsToSelector:@selector(standardWindowButton:)]) {
id closeButton = [window standardWindowButton:2];
if ([closeButton respondsToSelector:@selector(setEnabled:)]) {
[closeButton setEnabled:NO];
}
}
}
@end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句