2016-03-06 144 views
0

给定3个控制器:A,B,C A有一个隐藏的导航栏。通过StoryboardReference调用控制器B. 控制器B在viewDidLoad上显示导航栏。它有一个搜索栏和一个collectionView。查看我的故事板的屏幕截图A.如果单元格被点击,调用控制器C.导航栏后面的搜索栏

问题: 如果A调用B,则搜索栏位于导航栏后面(屏幕截图B)。它随着从C到B的过渡而出现(截图C)。

导航栏已经是半透明的。有任何想法吗?

编辑

我意识到,我的动画过渡引起了我的问题。

也许你可以发现错误?

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 
    } 
} 
+0

尝试在viewDidLoad – muazhud

+0

中设置'self.edgesForExtendedLayout = .None'它已经设置... – netshark1000

回答

1

我相信我遇到了我的应用程序中的一些类似问题,但由于所有您无法控制的事情而陷入困境。我的解决方案是在导航栏中放置一个搜索图标,然后让搜索控制器在导航栏上向下滑动,使其远离表格/滚动视图。下面是我实现的(应该是完整的)

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 
    } 
} 
+0

抱歉 - 但这没有帮助 – netshark1000

0

我觉得问题是你要么你是不是imageViewSnapshot设置框或设置错误的帧。由于imageViewSnapshot包含搜索栏,因此您必须设置框架,使其位于导航栏的后面。或imageViewSnapshot应该只包含fromViewTransitionFromView的可见区域。

+0

Searchbar是toViewController的一部分。 imageViewSnapshot动画开始处的fromViewController中的元素。我不明白为什么collectionview是可见的而搜索栏没有。 – netshark1000