         
         if navigationBar.isTranslucent {
             if #available(iOS 10.0, *){
-                if navigationBar.backgroundImage(for: .default) == nil {
-                    if let backgroundEffectView = barBackgroundView.value(forKey: "_backgroundEffectView") as? UIView {
-                        backgroundEffectView.alpha = alpha
-                        return
-                    }
-                }
-
+                guard navigationBar.backgroundImage(for: .default) == nil else {
+                    return
+                }
+                
+                if let backgroundEffectView = barBackgroundView.value(forKey: "_backgroundEffectView") as? UIView {
+                    backgroundEffectView.alpha = alpha
+                    return
+                }
+                
             }else{
-                if let adaptiveBackdrop = barBackgroundView.value(forKey: "_adaptiveBackdrop") as? UIView {
-                    if let backdropEffectView = adaptiveBackdrop.value(forKey: "_backdropEffectView") as? UIView {
-                        backdropEffectView.alpha = alpha
-                        return
-                    }
-                }
-            }
-            
-
+                guard let adaptiveBackdrop = barBackgroundView.value(forKey: "_adaptiveBackdrop") as? UIView else {
+                    return
+                }
+                
+                if let backdropEffectView = adaptiveBackdrop.value(forKey: "_backdropEffectView") as? UIView {
+                    backdropEffectView.alpha = alpha
+                    return
+                }
+            }
         }
         
         barBackgroundView.alpha = alpha
-        
-    }
-    
-    
-}
-
-extension UINavigationController:UINavigationControllerDelegate,UINavigationBarDelegate {
-
+    }
+}
+
+extension UINavigationController: UINavigationControllerDelegate,UINavigationBarDelegate {
+    
     public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
-        if let topVC = navigationController.topViewController {
-            if let coor = topVC.transitionCoordinator {
-                if #available(iOS 10.0, *) {
-                    coor.notifyWhenInteractionChanges({ (context) in
-                        self.dealInteractionChanges(context)
-                    })
-                } else {
-                    coor.notifyWhenInteractionEnds({ (context) in
-                        self.dealInteractionChanges(context)
-                    })
-                    
-                }
-            }
-        }
-    }
-    
-    private func dealInteractionChanges(_ context:UIViewControllerTransitionCoordinatorContext) {
+        guard let topVC = navigationController.topViewController, let coor = topVC.transitionCoordinator else {
+            return
+        }
+        
+        if #available(iOS 10.0, *) {
+            coor.notifyWhenInteractionChanges({ (context) in
+                self.dealInteractionChanges(context)
+            })
+        } else {
+            coor.notifyWhenInteractionEnds({ (context) in
+                self.dealInteractionChanges(context)
+            })
+        }
+    }
+    
+    private func dealInteractionChanges(_ context: UIViewControllerTransitionCoordinatorContext) {
+        let animations: (UITransitionContextViewControllerKey) -> () = {
+            let nowAlpha = context.viewController(forKey: $0)?.navBarBgAlpha ?? 0
+            self.setNeedsNavigationBackground(alpha: nowAlpha)
+            
+            self.navigationBar.tintColor = context.viewController(forKey: $0)?.navBarTintColor
+        }
+        
         if context.isCancelled {
-            let cancellDuration:TimeInterval = context.transitionDuration * Double( context.percentComplete)
+            let cancellDuration: TimeInterval = context.transitionDuration * Double(context.percentComplete)
             UIView.animate(withDuration: cancellDuration, animations: {
-                
-                let nowAlpha = (context.viewController(forKey: .from)?.navBarBgAlpha)!
-                self.setNeedsNavigationBackground(alpha: nowAlpha)
-                
-                self.navigationBar.tintColor = context.viewController(forKey: .from)?.navBarTintColor
-            })
-        }else{
-            let finishDuration:TimeInterval = context.transitionDuration * Double(1 - context.percentComplete)
+                animations(.from)
+            })
+        } else {
+            let finishDuration: TimeInterval = context.transitionDuration * Double(1 - context.percentComplete)
             UIView.animate(withDuration: finishDuration, animations: {
-                let nowAlpha = (context.viewController(forKey: .to)?.navBarBgAlpha)!
-                self.setNeedsNavigationBackground(alpha: nowAlpha)
-                
-                self.navigationBar.tintColor = context.viewController(forKey: .to)?.navBarTintColor
-            })
-        }
-    }
-    
-    
+                animations(.to)
+            })
+        }
+    }
     
     public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
-        if let topVC = topViewController {
-            if let coor = topVC.transitionCoordinator {
-                if coor.initiallyInteractive {
-                    return true
-                }
-            }
-        }
-        
-        
-        var popToVC: UIViewController?
-        if viewControllers.count >= (navigationBar.items?.count)! {
-            popToVC = viewControllers[viewControllers.count-2]
-        }else{
-            popToVC = viewControllers[viewControllers.count-1]
-        }
-        
-        if popToVC != nil {
-            _ = self.popToViewController(popToVC!, animated: true)
+        if let topVC = topViewController, let coor = topVC.transitionCoordinator, coor.initiallyInteractive {
             return true
         }
         
-        return false
+        let itemCount = navigationBar.items?.count ?? 0
+        let n = viewControllers.count >= itemCount ? 2 : 1
+        let popToVC = viewControllers[viewControllers.count - n]
+        
+        popToViewController(popToVC, animated: true)
+        
+        return true
     }
     
     public func navigationBar(_ navigationBar: UINavigationBar, shouldPush item: UINavigationItem) -> Bool {
-        setNeedsNavigationBackground(alpha: (topViewController?.navBarBgAlpha)!)
+        setNeedsNavigationBackground(alpha: topViewController?.navBarBgAlpha ?? 0)
         navigationBar.tintColor = topViewController?.navBarTintColor
         return true
     }
-    
-}
-
-
-
+}
 
 extension UIViewController {
     
     fileprivate struct AssociatedKeys {
         static var navBarBgAlpha: CGFloat = 1.0
-        static var navBarTintColor: UIColor = UIColor.defaultNavBarTintColor()
+        static var navBarTintColor: UIColor = UIColor.defaultNavBarTintColor
     }
     
     open var navBarBgAlpha: CGFloat {
         get {
-            let alpha = objc_getAssociatedObject(self, &AssociatedKeys.navBarBgAlpha) as? CGFloat
-            if alpha == nil {
+            
+            guard let alpha = objc_getAssociatedObject(self, &AssociatedKeys.navBarBgAlpha) as? CGFloat else {
                 return 1.0
-            }else{
-                return alpha!
-            }
+            }
+            return alpha
             
         }
         set {

        return topViewController?.preferredStatusBarStyle ?? .default
    }
    
    open override class func initialize(){
        
        if self == UINavigationController.self {
            
            let needSwizzleSelectorArr = [["ori":NSSelectorFromString("_updateInteractiveTransition:"),"swi":NSSelectorFromString("et_updateInteractiveTransition:")],
                ["ori":#selector(popToViewController(_:animated:)),"swi":#selector(et_popToViewController(_:animated:))],
                ["ori":#selector(popToRootViewController(animated:)),"swi":#selector(et_popToRootViewController(animated:))]]
            
            for needSwizzleSelector in needSwizzleSelectorArr {
                let originalSelector = needSwizzleSelector["ori"]
                let swizzledSelector = needSwizzleSelector["swi"]
                let originalMethod = class_getInstanceMethod(self.classForCoder(), originalSelector)
                let swizzledMethod = class_getInstanceMethod(self.classForCoder(), swizzledSelector)
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
            
        }
        
    }
    

    func et_updateInteractiveTransition(_ percentComplete: CGFloat) {
        et_updateInteractiveTransition(percentComplete)
        if let topVC = self.topViewController {
            if let coor = topVC.transitionCoordinator {
                //Bg Alpha
                let fromAlpha = coor.viewController(forKey: .from)?.navBarBgAlpha
                let toAlpha = coor.viewController(forKey: .to)?.navBarBgAlpha
                let nowAlpha = fromAlpha! + (toAlpha!-fromAlpha!)*percentComplete
                
                self.setNeedsNavigationBackground(alpha: nowAlpha)
                
                //tintColor
                let fromColor = coor.viewController(forKey: .from)?.navBarTintColor
                let toColor = coor.viewController(forKey: .to)?.navBarTintColor
                let nowColor = averageColor(fromColor: fromColor!, toColor: toColor!, percent: percentComplete)
                self.navigationBar.tintColor = nowColor
            }
        }
    }
    
    //Calculate the middle Color with translation percent
    private func averageColor(fromColor:UIColor, toColor:UIColor, percent:CGFloat) -> UIColor {
        var fromRed :CGFloat = 0.0
        var fromGreen :CGFloat = 0.0
        var fromBlue :CGFloat = 0.0
        var fromAlpha :CGFloat = 0.0
        fromColor.getRed(&fromRed, green: &fromGreen, blue: &fromBlue, alpha: &fromAlpha)
        
        var toRed :CGFloat = 0.0

            objc_setAssociatedObject(self, &AssociatedKeys.navBarTintColor, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}
