jdg / MBProgressHUD

MBProgressHUD + Customizations
http://www.bukovinski.com/
MIT License
16.01k stars 3.56k forks source link

Crashes because of constraint issues between MBProgressHud and child custom view. #660

Open albertB4work opened 6 months ago

albertB4work commented 6 months ago

This is the crash reported in Firebase after several workaround tried

Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x300183b10 RTSpinKitView:0x115da6480.centerX == MBBackgroundView:0x127179510.centerX (active)> view:<MBBackgroundView: 0x127179510; frame = (0 0; 0 0); clipsToBounds = YES; alpha = 0; animations = { opacity=<CASpringAnimation: 0x302693b00>; }; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x30267b720>>

This is the only class where the library is used, the only methods:

func showLoading() {
        if !isAnimating {
            isAnimating = true
            hud = MBProgressHUD.showAdded(to: self.view, animated: true)

            guard let spinner = spinner, let hud = hud else {
                // Make sure spinner and hud are not nil
                return
            }

            hud.bezelView.color = .clear
            hud.bezelView.style = .solidColor
            hud.isSquare = true
            hud.mode = .customView
            hud.removeFromSuperViewOnHide = true
            hud.customView = spinner

            // Make sure spinner is a subview of hud and hud has height and width
            guard spinner.isDescendant(of: hud), hud.frame.height > 0, hud.frame.width > 0 else {
                return
            }

            NSLayoutConstraint.activate([
                spinner.widthAnchor.constraint(equalToConstant: 50),
                spinner.heightAnchor.constraint(equalToConstant: 50),
                spinner.centerXAnchor.constraint(equalTo: hud.centerXAnchor),
                spinner.centerYAnchor.constraint(equalTo: hud.centerYAnchor),
            ])
            spinner.startAnimating()
        }
    }

    func dismissLoading() {
        isAnimating = false
        MBProgressHUD.hide(for: self.view, animated: true)
    }

Any other workaround to avoid these kind of crash? It's the more reported crash in Crashlytics. Thank you!