iOS 应用获取最上层 Window 的正确方法
2017-09-30 本文已影响0人
维若
今天帮同事看bug,看到他用[[UIApplication sharedApplication].windows lastobject]来获取最上层window,添加view后,不显示。
demo地址:https://github.com/xuaimian/TopLevelWindow
所以整理了下面的方法:
方法一:
-(UIWindow *)topLevelWindowGet
{
UIWindow *topView = [UIApplication sharedApplication].keyWindow;
for (UIWindow *win in [[UIApplication sharedApplication].windows reverseObjectEnumerator]) {
if ([win isEqual: topView]) {
continue;
}
if (win.windowLevel > topView.windowLevel && win.hidden != YES ) {
topView =win;
}
}
return topView;
}
方法二:
UIWindow *topView = [UIApplication sharedApplication].keyWindow;
[topView endEditing:Yes];
return topView;