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

时间:2018-05-04 14:35:07

标签: ios swift uiviewcontroller uinavigationbar uisearchbar

我有两个嵌入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后,搜索控制器从层次结构中删除,我得到一个空白空格。以下是演示:

enter image description here

当我在控制台上打印UISearchController或搜索栏时,我会获得对象信息,但是,正如您所看到的,它会从视图层次结构中消失(在视图层次结构调试器中我可以'找到它)。

为什么会发生这种情况,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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

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

viewDidLoad

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

enter image description here