blog

UIWindowを取得する

注: 以下のものをそのまま使うのがよいでしょう。特にiOS11以降は。 3、UIWindowのプロパティを導入します。...

Dec 8, 2020 · 2 min. read

注:以下のものを使用するのが良いでしょう。特にiOS 11以降は。

[UIApplication sharedApplication].keyWindow;

1.以下は、より厳密なアプローチです。

- (UIWindow *)lastWindow { NSArray *windows = [UIApplication sharedApplication].windows; for(UIWindow *window in [windows reverseObjectEnumerator]) { if ([window isKindOfClass:[UIWindow class]] && CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds)) return window; } return [UIApplication sharedApplication].keyWindow; }

2、単に使用するだけでなく

[[UIApplication sharedApplication].windows lastObject];

3.UIWindowのプロパティ、windowLevelを紹介します。 Appleのドキュメントでは、windowLevelには3つの形式があり、それらはCGFloat型です。

UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal; UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert; UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar __TVOS_PROHIBITED;

まず、アップルのヘッダーファイルで、UIWindowのwindowLevelが何であるかを確認します。

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSLog(@"
UIWindowLevelNormal = %f 
UIWindowLevelAlert = %f 
UIWindowLevelStatusBar = %f 
appDelegate.window.windowLevel = %f",
UIWindowLevelNormal,
UIWindowLevelAlert,
UIWindowLevelStatusBar,
appDelegate.window.windowLevel);
結果を確認する
UIWindowLevelNormal = 0. 
UIWindowLevelAlert = 0 
UIWindowLevelStatusBar = 0 
appDelegate.window.windowLevel = 0.
Read next

iOS 14でSwiftUIアプリにAppDelegateを追加するには?

iOS 14では、SwiftUIアプリはAppプロトコルに従いますが、そうではなく、いくつかのシナリオでは、プッシュ登録、メモリ警告の処理など、古いもののライフサイクル関数を使用する必要があります。 ここで ...

Dec 7, 2020 · 1 min read