Skip to content

Commit b61e265

Browse files
Alexei GridnevAlexei Gridnev
Alexei Gridnev
authored and
Alexei Gridnev
committedNov 4, 2023
Avoid crashes when SCLAlertView is called from viewDidLoad
1 parent 5643822 commit b61e265

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed
 

‎SCLAlertView/SCLAlertView.swift

+20-4
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ open class SCLAlertView: UIViewController {
401401
return
402402
}
403403

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!
405406
let sz = rv.frame.size
406407

407408
// Set background frame
@@ -754,7 +755,8 @@ open class SCLAlertView: UIViewController {
754755
view.alpha = 0
755756
view.tag = uniqueTag
756757
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!
758760
rv.addSubview(view)
759761
view.frame = rv.bounds
760762
baseView.frame = rv.bounds
@@ -873,7 +875,8 @@ open class SCLAlertView: UIViewController {
873875
// Show animation in the alert view
874876
fileprivate func showAnimation(_ animationStyle: SCLAnimationStyle = .topToBottom, animationStartOffset: CGFloat = -400.0, boundingAnimationOffset: CGFloat = 15.0, animationDuration: TimeInterval = 0.2) {
875877

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!
877880
var animationStartOrigin = self.baseView.frame.origin
878881
var animationCenter : CGPoint = rv.center
879882

@@ -902,16 +905,29 @@ open class SCLAlertView: UIViewController {
902905

903906
self.baseView.frame.origin = animationStartOrigin
904907

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+
905914
if self.appearance.dynamicAnimatorActive {
906915
UIView.animate(withDuration: animationDuration, animations: {
907916
self.view.alpha = 1.0
908-
})
917+
}) { _ in
918+
if bringViewToFront {
919+
rv.bringSubviewToFront(self.view)
920+
}
921+
}
909922
self.animate(item: self.baseView, center: rv.center)
910923
} else {
911924
UIView.animate(withDuration: animationDuration, animations: {
912925
self.view.alpha = 1.0
913926
self.baseView.center = animationCenter
914927
}, completion: { finished in
928+
if bringViewToFront {
929+
rv.bringSubviewToFront(self.view)
930+
}
915931
UIView.animate(withDuration: animationDuration, animations: {
916932
self.view.alpha = 1.0
917933
self.baseView.center = rv.center

0 commit comments

Comments
 (0)
Please sign in to comment.