@@ -401,7 +401,8 @@ open class SCLAlertView: UIViewController {
401
401
return
402
402
}
403
403
404
- let rv = UIApplication . shared. windows. filter ( { $0. isKeyWindow} ) . first! as UIWindow
404
+ let rv = UIApplication . shared. windows. filter ( { $0. isKeyWindow} ) . first ??
405
+ UIApplication . shared. windows. first!
405
406
let sz = rv. frame. size
406
407
407
408
// Set background frame
@@ -754,7 +755,8 @@ open class SCLAlertView: UIViewController {
754
755
view. alpha = 0
755
756
view. tag = uniqueTag
756
757
view. accessibilityIdentifier = uniqueAccessibilityIdentifier
757
- let rv = UIApplication . shared. windows. filter ( { $0. isKeyWindow} ) . first! as UIWindow
758
+ let rv = UIApplication . shared. windows. filter ( { $0. isKeyWindow} ) . first ??
759
+ UIApplication . shared. windows. first!
758
760
rv. addSubview ( view)
759
761
view. frame = rv. bounds
760
762
baseView. frame = rv. bounds
@@ -873,7 +875,8 @@ open class SCLAlertView: UIViewController {
873
875
// Show animation in the alert view
874
876
fileprivate func showAnimation( _ animationStyle: SCLAnimationStyle = . topToBottom, animationStartOffset: CGFloat = - 400.0 , boundingAnimationOffset: CGFloat = 15.0 , animationDuration: TimeInterval = 0.2 ) {
875
877
876
- let rv = UIApplication . shared. windows. filter ( { $0. isKeyWindow} ) . first! as UIWindow
878
+ let rv = UIApplication . shared. windows. filter ( { $0. isKeyWindow} ) . first ??
879
+ UIApplication . shared. windows. first!
877
880
var animationStartOrigin = self . baseView. frame. origin
878
881
var animationCenter : CGPoint = rv. center
879
882
@@ -902,16 +905,29 @@ open class SCLAlertView: UIViewController {
902
905
903
906
self . baseView. frame. origin = animationStartOrigin
904
907
908
+ // When people call SCLAlertView from viewDidLoad of their root UIViewController
909
+ // on the app start we many end up with a non-key window and later our view will be covered
910
+ // by the view controller's view.
911
+ // The best we can do is to bring our view to front later.
912
+ let bringViewToFront = !rv. isKeyWindow
913
+
905
914
if self . appearance. dynamicAnimatorActive {
906
915
UIView . animate ( withDuration: animationDuration, animations: {
907
916
self . view. alpha = 1.0
908
- } )
917
+ } ) { _ in
918
+ if bringViewToFront {
919
+ rv. bringSubviewToFront ( self . view)
920
+ }
921
+ }
909
922
self . animate ( item: self . baseView, center: rv. center)
910
923
} else {
911
924
UIView . animate ( withDuration: animationDuration, animations: {
912
925
self . view. alpha = 1.0
913
926
self . baseView. center = animationCenter
914
927
} , completion: { finished in
928
+ if bringViewToFront {
929
+ rv. bringSubviewToFront ( self . view)
930
+ }
915
931
UIView . animate ( withDuration: animationDuration, animations: {
916
932
self . view. alpha = 1.0
917
933
self . baseView. center = rv. center
0 commit comments