导航栏后面的搜索栏 [英] Searchbar behind Navigationbar

查看:72
本文介绍了导航栏后面的搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出3个控制器:A,B,C A具有隐藏的导航栏.通过StoryboardReference调用控制器B. 控制器B在viewDidLoad上显示导航栏.它具有一个搜索栏和一个collectionView.请参阅我的情节提要的屏幕快照A.如果单击一个单元格,则调用控制器C.

Given 3 Controllers: A,B,C A has a hidden navigationbar. Calls Controller B via a StoryboardReference. Controller B shows Navigationbar on viewDidLoad. It has a searchbar and a collectionView. See screenshot A of my storyboard. Calls controller C if a cell is clicked.

问题: 如果A呼叫B,则搜索栏位于导航栏的后面(屏幕快照B).从C到B的过渡出现了(屏幕截图C).

Problem: If A calls B the searchbar is behind the navigationbar (Screenshot B). It appears with the transition from C to B (Screenshot C).

导航栏已经是半透明的.有什么想法吗?

Navigationbar is already translucent. Any ideas?

编辑

我意识到动画过渡导致了我的问题.

I realized that my animated transitioning is causing my problem.

也许您可以发现错误?

class ZoomInCircleViewTransition: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    var transitionContext: UIViewControllerContextTransitioning?

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.6
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext

        guard let toViewController: UIViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else {
            return
        }

        guard let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) else {            return
        }

        guard let fromViewTransitionFromView = fromViewController as? TransitionFromViewProtocol else {
            return
        }

        let imageViewSnapshot = fromViewTransitionFromView.getViewForTransition()

        let endFrame = CGRectMake(-CGRectGetWidth(toViewController.view.frame)/2, -CGRectGetHeight(toViewController.view.frame)/2, CGRectGetWidth(toViewController.view.frame)*2, CGRectGetHeight(toViewController.view.frame)*2)

        if let containerView = transitionContext.containerView(){
            containerView.addSubview(fromViewController.view)
            containerView.addSubview(toViewController.view)
            containerView.addSubview(imageViewSnapshot)
        }


        let maskPath = UIBezierPath(ovalInRect: imageViewSnapshot.frame)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = toViewController.view.frame
        maskLayer.path = maskPath.CGPath
        toViewController.view.layer.mask = maskLayer

        let quadraticEndFrame = CGRect(x: endFrame.origin.x - (endFrame.height - endFrame.width)/2, y: endFrame.origin.y, width: endFrame.height, height: endFrame.height)
        let bigCirclePath = UIBezierPath(ovalInRect: quadraticEndFrame)

        let pathAnimation = CABasicAnimation(keyPath: "path")
        pathAnimation.delegate = self
        pathAnimation.fromValue = maskPath.CGPath
        pathAnimation.toValue = bigCirclePath
        pathAnimation.duration = transitionDuration(transitionContext)
        maskLayer.path = bigCirclePath.CGPath
        maskLayer.addAnimation(pathAnimation, forKey: "pathAnimation")


        let hideImageViewAnimation =  {
            imageViewSnapshot.alpha = 0.0
        }

        UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: hideImageViewAnimation) { (completed) -> Void in
        }

        let scaleImageViewAnimation = {
            imageViewSnapshot.frame = quadraticEndFrame
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: scaleImageViewAnimation) { (completed) -> Void in
            // After the complete animations have endet
            imageViewSnapshot.removeFromSuperview()
            toViewController.view.layer.mask = nil
        }
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        if let transitionContext = self.transitionContext {
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    }

    // MARK: UIViewControllerTransitioningDelegate protocol methods

    // return the animataor when presenting a viewcontroller
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    // return the animator used when dismissing from a viewcontroller
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

推荐答案

我认为我的应用程序遇到了一些类似的问题,但是由于您无法控制的所有事情而遇到了困难.我的解决方案是在导航栏中放置一个搜索图标,然后使搜索控制器在导航栏上向下滑动,以使其保持在表格/滚动视图之外.这是我的实现(应该完整)

I believe I was running in to some similar problems in my app, but running in to difficulties because of all the things you don't have control over. My solution was to put a search icon in the navigationbar, then have the search controller slide down over the navigationbar, keeping it out of the table/scroll view. Here is my implementation (should be complete)

import UIKit

class tvc: UITableViewController, UISearchBarDelegate, UISearchControllerDelegate {

var searchController:UISearchController!

@IBAction func startSearch() {
    self.navigationController?.presentViewController(self.searchController, animated: true, completion: {})
}

override func viewDidDisappear(animated: Bool) {
    cancelSearch(self)
}

override func viewDidLoad() {
    super.viewDidLoad()

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.delegate = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.loadViewIfNeeded()  /* Fixes bug in iOS http://stackoverflow.com/questions/32675001/uisearchcontroller-warning-attempting-to-load-the-view-of-a-view-controller */
    definesPresentationContext = true
    tableView.sectionIndexBackgroundColor = UIColor.clearColor()
    tableView.sectionIndexTrackingBackgroundColor = UIColor.clearColor()
}


extension tvc: UISearchResultsUpdating {
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResultsForSearchController(searchController: UISearchController) {
    filterContentForSearchText(searchController.searchBar.text!)
    }
}


func cancelSearch(sender: AnyObject?) {
if sender!.searchController.active == true {
sender?.searchController.resignFirstResponder()
sender!.navigationController!!.dismissViewControllerAnimated(false, completion: {})
sender!.searchController.searchBar.text = ""
sender!.searchController.active = false
    }
}

这篇关于导航栏后面的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆