Ios 使用动画更改rootController时崩溃

Ios 使用动画更改rootController时崩溃,ios,animation,uistackview,Ios,Animation,Uistackview,我使用此功能更改实际的应用程序根控制器: class func setRootController(newController: UIViewController, animation: UIViewAnimationOptions? = nil) { let appDelegate = UIApplication.shared.delegate as! AppDelegate if animation != nil { let currentControl

我使用此功能更改实际的应用程序根控制器:

class func setRootController(newController: UIViewController, animation: UIViewAnimationOptions? = nil) {

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    if animation != nil {

        let currentController = appDelegate.window!.rootViewController!

        UIView.transition(from: currentController.view, to: newController.view, duration: 0.6, options: animation!, completion: { (completed) in
            appDelegate.window?.rootViewController = newController
        })


    } else {
        appDelegate.window?.rootViewController = newController
    }
}
我的控制器将出现(使用setRoot时调用两次:1-创建控制器并开始动画时,2-在完成块中):

我的balanceView(只是一个UIView)包含bonusesStack(stackView)和didSet的var奖金:

var bonuses: [Bonus]! {
    didSet {

        for subview in bonusesStack.arrangedSubviews {
            bonusesStack.removeArrangedSubview(subview)
            subview.removeFromSuperview()
        }

        bonuses.forEach { (bonus) in
            let bonusView = BonusBalanceView.loadFromXib(bonus: bonus)

            bonusesStack.addArrangedSubview(bonusView)
        }
    }
}

class func loadFromXib(bonus: Bonus) -> BonusBalanceView {
    let bonusView = Bundle.main.loadNibNamed(String(describing: BonusBalanceView.self), owner: nil, options: nil)?.first as! BonusBalanceView
    // some code here
    return bonusView
}
当第二次调用WILLISPEND时,代码在第
行的子视图中崩溃。removeFromSuperview()
(第二次调用bcs时,superview为零。我尝试添加if块以进行superview检查,但由于某些原因,它总是执行O_O)


如果我注释/删除这一行(不建议这样做,bcs视图仍在stackView子视图中),则代码在完成块中崩溃

问题出在我的自定义视图中。我重写了draw方法并以编程方式处理图层。在xib文件中,此视图包含子视图。不知何故(“”\_(ツ)_/“”)此层和子视图冲突和崩溃应用程序

var bonuses: [Bonus]! {
    didSet {

        for subview in bonusesStack.arrangedSubviews {
            bonusesStack.removeArrangedSubview(subview)
            subview.removeFromSuperview()
        }

        bonuses.forEach { (bonus) in
            let bonusView = BonusBalanceView.loadFromXib(bonus: bonus)

            bonusesStack.addArrangedSubview(bonusView)
        }
    }
}

class func loadFromXib(bonus: Bonus) -> BonusBalanceView {
    let bonusView = Bundle.main.loadNibNamed(String(describing: BonusBalanceView.self), owner: nil, options: nil)?.first as! BonusBalanceView
    // some code here
    return bonusView
}