自定义转换后搜索栏从视图层次结构中删除

Search bar gets removed from the view hierarchy after custom transition

我在 UINavigationController 中嵌入了两个视图控制器。第一个视图控制器将 UISearchController 设置为其导航项。这是我配置搜索控制器的完整代码:

 private func configureSearchController() {
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.hidesNavigationBarDuringPresentation = false
    //searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.tintColor = .white
    searchController.searchBar.delegate = self
    //White search text
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
    //White placeholder
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search", comment: "search bar placeholder"), attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

    //searchController.searchBar.sizeToFit()
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false

    navigationController?.navigationBar.prefersLargeTitles = false
    //
    definesPresentationContext = true
}

我从 viewDidLoad 调用了这个方法。

如问题标题所述,我使用导航控制器自定义过渡。这是过渡动画师的完整代码。

  class RevealViewControllerAnimator: NSObject,    UIViewControllerAnimatedTransitioning {

private let animationDuration = 1.5
var operation: UINavigationControllerOperation = .push
var isShowing = true
private weak var storedContext: UIViewControllerContextTransitioning?
var snapshot: UIView?
private lazy var viewOnTopOfSnapshot: UIView? = {
    let view = UIView()
    view.frame = self.snapshot!.frame
    if isShowing {
        view.backgroundColor = .clear
    } else {
        view.backgroundColor = UIColor(white: 0.3, alpha: 0.4)
    }
    return view
}()

private var backgroundViewBackgroundDarkColor =  UIColor(white: 0.2, alpha: 0.4)


func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return animationDuration
}


func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    storedContext = transitionContext
    print ("OPERATION", operation.rawValue)

    //If we are presenting a view controller
    if isShowing {
        let fromVC = transitionContext.viewController(forKey: .from) as! ViewController1
        let toVC = transitionContext.viewController(forKey: .to) as! ViewController2

        snapshot = UIApplication.shared.keyWindow?.snapshotView(afterScreenUpdates: false)


        let containerView = transitionContext.containerView

        //Adding a view on top of a snapshot and animating its bacground color
        if let snapshot = snapshot, let viewOnTopOfSnapshot = viewOnTopOfSnapshot {
            containerView.addSubview(self.snapshot!)
            containerView.insertSubview(viewOnTopOfSnapshot, aboveSubview: snapshot)

            UIView.animate(withDuration: animationDuration - 1.0, animations: {
                viewOnTopOfSnapshot.backgroundColor = self.backgroundViewBackgroundDarkColor


            }, completion: nil)
        }


        containerView.addSubview(toVC.view)
        toVC.view.frame = transitionContext.finalFrame(for: toVC)

        animate(toView: toVC.view, fromTriggerButton: fromVC.filterButton)


    } else {
        //If we are dismissing the view controller

        let fromVC = transitionContext.viewController(forKey: .from) as! ViewController2
        let toVC = transitionContext.viewController(forKey: .to) as! ViewController1
        let containerView = transitionContext.containerView


        //Animating the background color change to clear
        if let viewOnTopOfSnapshot = viewOnTopOfSnapshot {

            UIView.animate(withDuration: animationDuration, animations: {
                viewOnTopOfSnapshot.backgroundColor = .clear
            }, completion: {_ in
                self.snapshot?.removeFromSuperview()
                viewOnTopOfSnapshot.removeFromSuperview()

            })
        }

        //containerView.addSubview(fromVC.view)
        containerView.insertSubview(toVC.view!, belowSubview: snapshot!)
        animateDismisss(fromView: fromVC.view, toTriggerButton: fromVC.saveButton)


    }
}


//MARK: Animation for pushing
private func animate(toView: UIView, fromTriggerButton button: UIButton) {

    let rect = CGRect(x: toView.frame.maxX, y: toView.frame.minY, width: button.frame.width, height: button.frame.height)

    let circleMaskPathInitial = UIBezierPath(ovalIn: rect)

    let fullHeight = toView.bounds.height
    let extremePoint = CGPoint(x: button.center.x, y: button.center.y - fullHeight)
    let radius = sqrt((extremePoint.x * extremePoint.x) + (extremePoint.y * extremePoint.y))
    let circleMaskPathFinal = UIBezierPath(ovalIn: button.frame.insetBy(dx: -radius - 1000, dy: -radius - 1000))

    let maskLayer = CAShapeLayer()
    maskLayer.path = circleMaskPathFinal.cgPath
    toView.layer.mask = maskLayer

    let maskLayerAnimation = CABasicAnimation(keyPath: "path")
    maskLayerAnimation.fromValue = circleMaskPathInitial.cgPath
    maskLayerAnimation.toValue = circleMaskPathFinal.cgPath
    maskLayerAnimation.duration = animationDuration
    maskLayerAnimation.delegate = self

    maskLayer.add(maskLayerAnimation, forKey: "path")

}

//MARK: Animation for pop (dismiss)
private func animateDismisss(fromView: UIView, toTriggerButton button: UIButton) {

    let rect = CGRect(x: button.frame.origin.x, y: button.frame.midY, width: button.frame.width, height: button.frame.width)
    let finalCircleMaskPath = UIBezierPath(ovalIn: rect)

    let fullHeight = fromView.bounds.height
    let extremePoint = CGPoint(x: button.center.x, y: button.center.y - fullHeight)
    let radius = sqrt((extremePoint.x * extremePoint.x) + (extremePoint.y * extremePoint.y))
    let initialCircleMaskPath = UIBezierPath(ovalIn: button.frame.insetBy(dx: -radius, dy: -radius))

    let maskLayer = CAShapeLayer()
    maskLayer.path = finalCircleMaskPath.cgPath
    fromView.layer.mask = maskLayer

    let maskLayerAnimation = CABasicAnimation(keyPath: "path")
    maskLayerAnimation.fromValue = initialCircleMaskPath.cgPath
    maskLayerAnimation.toValue = finalCircleMaskPath.cgPath
    maskLayerAnimation.duration = 0.8
    maskLayerAnimation.delegate = self

    maskLayer.add(maskLayerAnimation, forKey: "path")
}


extension RevealFilterViewControllerAnimator : CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    if let context = storedContext {
        storedContext?.completeTransition(!context.transitionWasCancelled)

    } else {
        storedContext = nil
    }
  }
}

所以,用两个词来说,我得到了 ViewController1 的快照,将其插入到 containerView,然后在其顶部插入另一个视图,我在动画期间更改了背景颜色.弹出时,我摆脱了快照和视图,并将 ViewController1 的视图插入到 containerView

正如我在问题开头提到的,我在第一个视图控制器中有一个带有搜索栏的 UISearchController

问题是,在关闭 ViewController2 之后,搜索控制器从层次结构中删除,我得到一个空白 space。下面是演示:

当我在控制台上打印 UISearchController 或搜索栏时,我得到了 object 信息,但是,如您所见,它从视图层次结构中消失了(在视图层次结构中调试器我找不到它)。

为什么会这样,如何解决?

最后,我弄清楚了导致问题的原因,一旦弄清楚,解决方案就非常简单了。所以,发生这种情况的原因是在 ViewController2viewDidLoad 方法中我隐藏了导航栏,但在弹出视图控制器时我从未将其设置回去。

所以,这是我在第二个视图控制器中用于导航栏的代码(我的视图控制器看起来有点不同,但逻辑是相同的):

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationController?.setNavigationBarHidden(true, animated: false)
}

 override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    navigationController?.setNavigationBarHidden(false, animated: true)

}

这是动画现在的样子(我知道,这里有一些粗糙的边缘,但至少,问题解决了)。