一般最简单的方法
UIWindow *window = [UIapplication sharedApplication].keyWindow;或者UIWindow *window = [[UIApplication sharedApplication].windows lastObject];或者再对windows做一下排序判断
UIWindow *topWindow = [[[UIApplication sharedApplication].windows sortedArrayUsingComparator:^NSComparisonResult(UIWindow *win1, UIWindow *win2) { return win1.windowLevel - win2.windowLevel; }] lastObject];上面不管哪种方法,如果是在只有一个window的情形下倒没问题,但是当存在多个window,则都不能真正获取到当前正在显示的窗口 。终极方法如下:
UIWindow *topWindow = [[[UIApplication sharedApplication] delegate] window];2. 获取最顶层的视图控制器
不论中间采用了 push->push->PResent还是present->push->present,或是其它交互
(UIViewController *)theTopviewControler{ UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController; UIViewController *parent = rootVC; while ((parent = rootVC.presentedViewController) != nil ) { rootVC = parent; } while ([rootVC isKindOfClass:[UINavigationController class]]) { rootVC = [(UINavigationController *)rootVC topViewController]; } return rootVC;}3. 获取最顶层的视图
拿到视图控制器,获取视图就方便了
新闻热点
疑难解答