keyWindow获取出错,导致视频不能正常播放
程序员文章站
2024-01-15 08:10:58
...
- (void)showInWindow
{
//1.这种获取当前window的方式有风险 当弹出一个alertview [[UIApplication sharedApplication] keyWindow]获取到的是_UIAlertControllerShimPresenterWindow,当alertview消失,_UIAlertControllerShimPresenterWindow消失,加在之上的视图也会跟着消失
//2.其次,_UIAlertControllerShimPresenterWindow并不在[[UIApplication sharedApplication] windows]数组里
//3.[[UIApplication sharedApplication] keyWindow]获取的永远是最外层的window
UIWindow *keyWindow = [[[UIApplication sharedApplication] windows] firstObject];
[keyWindow addSubview:self.view];
self.view.alpha = 0.0;
[UIView animateWithDuration:kVideoPlayerControllerAnimationTimeInterval animations:^{
self.view.alpha = 1.0;
} completion:^(BOOL finished) {
}];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
1.经过测试keyWindow永远是覆盖在视图的最外层,在正常情况下获取到的keyWindow是UIWindow;
2.在有UIAlertView或UIActionSheet的弹出层情况下,在去获取keyWindow这个时keyWindow就会变成UIAlertControllerShimPresenterWindow这个类,是弹出层上层的一个window;
3.在UIAlertView或UIActionSheet弹出后再给keyWindow添加的view,会在UIAlertView弹出层消失后,keyWindow上边的视图也会随之消失;
所以如果要可以使用UIApplication.shared.windows.last(获取到的图层是UIWindow)去添加view,这样又弹出层的话还是添加到系统的弹出层下,不会随着弹出层的消失而消失。
下一篇: 更多箭头按钮不停闪烁animation