ios - 自定义转换后,搜索栏将从 View 层次结构中删除

标签 ios swift uiviewcontroller uinavigationbar uisearchbar

我有两个 View Controller 嵌入到 UINavigationController 中。第一个 View Controller 将 UISearchController 设置为其导航项。这是我配置搜索 Controller 的完整代码:

 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调用这个方法。

正如问题标题中提到的,我使用导航 Controller 自定义转换。这是过渡动画师的完整代码。

  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 中,并在其顶部插入另一个 View ,其背景颜色是我的动画期间发生变化。弹出时,我删除了快照和 View ,并将 ViewController1 的 View 插入到 containerView 中。

正如我在问题开头提到的,我在第一个 View Controller 中有一个带有搜索栏的 UISearchController

问题是,在关闭 ViewController2 后,搜索 Controller 将从层次结构中删除,并且我得到一个空白区域。以下是演示:

enter image description here

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

为什么会发生这种情况?如何解决?

最佳答案

最后,我找出了导致问题的原因,一旦找到,解决方案就非常简单了。所以,发生这种情况的原因是在 ViewController2 的 viewDidLoad 方法中我隐藏了导航栏,但在弹出 View Controller 时我从未将其设置回来。

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

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)

}

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

enter image description here

关于ios - 自定义转换后,搜索栏将从 View 层次结构中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50177425/

相关文章:

ios - 在 UITableViewCell 中下载和缓存图像

ios - UIViewController模糊背景变成灰色

swift - 由于 UIImageView 和 UITextField 导致 NSUnknownKeyException 错误

iOS - 本地化 Storyboard Scene/ViewController 的最佳架构

ios - 将手势识别器实现为两个 View Controller 之间的 Segue

ios - 如何让数据管理器告诉 View Controller 重新加载集合

ios - ios内购可以使用第三方支付网关吗?

iphone - 如何确定当前位置是否在 KML 定义的区域内?

ios - UIView原点不等于0

ios - 导航堆栈中前一个 View Controller 的快速访问属性