注:以下のものを使用するのが良いでしょう。特に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.