2016-03-06 146 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