2015-05-05 6 views
5

Ich bin wirklich hier fest. Ich möchte die Navigationsleiste verkleinern, wenn ich einen UITableView nach unten scrolle und ihn beim Scrollen nach oben wieder vergrößere. Ich habe es geschafft, die Größe der Navigationsleiste zu ändern, aber das Titelbild schrumpft nicht mit der Navigationsleiste.Swift - Shrink Navigationsleiste auf Scroll

Ich möchte es genau wie Safari tun, und das Problem ist, dass die Höhe meiner Titelansicht schrumpft, aber die Breite ändert sich nie.

Hier ist der Code, den ich verwendet habe, um die Höhe der Bildlaufleiste zu ändern.

func scrollViewDidScroll(scrollView: UIScrollView) { 
     var navbar = navigationController?.navigationBar 
     var dir:CGPoint = tableview.panGestureRecognizer.translationInView(self.tableview) 
     var scrollViewHeight = tableview.frame.size.height 
     var scrollContentSizeHeight = tableview.contentSize.height 
     var scrollOffset = tableview.contentOffset.y 

     if (dir.y > 0 && self.formernavstate == "small") { 
      self.formernavstate = "big" 

      UIView.animateWithDuration(0.5, delay:0.0, options: UIViewAnimationOptions.AllowAnimatedContent, animations: {() -> Void in 

      println("") 
      navbar?.frame.origin.y = 20 
      self.navigationItem.titleView?.transform = CGAffineTransformMakeScale(0.52, 0.6) 
      }, completion: nil) 
     } 

     if (dir.y < 0 && self.formernavstate == "big") { 
      self.formernavstate = "small" 
      navbar?.frame.origin.y = 0 
      navigationItem.titleView?.transform = CGAffineTransformMakeScale(0.0001, 0.2) 
     } 
    } 

Antwort

1

// einfach kopieren und hinter diesem Code

var lastContentOffsetAtY : CGFloat = 0.0 

func scrollViewWillBeginDragging(scrollView: UIScrollView) { 
    self.lastContentOffsetAtY = scrollView.contentOffset.y 
} 

func scrollViewDidScroll(scrollView: UIScrollView) { 
    if lastContentOffsetAtY < scrollView.contentOffset.y { 
     print("Bottom") 
     //self.navigationController?.setNavigationBarHidden(true, animated: true) 
     shrinkTheCustomNavigationBar(isShrink: true) 
    } else if lastContentOffsetAtY > scrollView.contentOffset.y { 
     print("Top") 
     //self.navigationController?.setNavigationBarHidden(false, animated: true) 
     shrinkTheCustomNavigationBar(isShrink: false) 
    } 
} 

func shrinkTheCustomNavigationBar(isShrink isShrink:Bool) { 
    if isShrink == true{ 
     if self.navigationController?.navigationBar.frame.size.height > 22.0{ 
      UIView.animateWithDuration(0.1, animations: { 
       self.navigationController?.navigationBar.frame = CGRectMake((self.navigationController?.navigationBar.frame.origin.x)!, (self.navigationController?.navigationBar.frame.origin.y)!, (self.navigationController?.navigationBar.frame.size.width)!, (self.navigationController?.navigationBar.frame.size.height)! - 3) 
       self.view.layoutIfNeeded() 
       self.navigationController?.navigationItem.titleView?.frame = CGRectMake(0, 0, 320, (self.navigationController?.navigationBar.frame.size.height)!) 
       self.navigationController?.navigationItem.titleView = self.titleLable 
       }, completion: { (success) in 
      }) 
     } 
    } else { 
     if self.navigationController?.navigationBar.frame.size.height < 44.0{ 
      UIView.animateWithDuration(0.1, animations: { 
       self.navigationController?.navigationBar.frame = CGRectMake((self.navigationController?.navigationBar.frame.origin.x)!, (self.navigationController?.navigationBar.frame.origin.y)!, (self.navigationController?.navigationBar.frame.size.width)!, (self.navigationController?.navigationBar.frame.size.height)! + 3) 
       self.view.layoutIfNeeded() 
       self.navigationController?.navigationItem.titleView?.frame = CGRectMake(0, 0, 320, (self.navigationController?.navigationBar.frame.size.height)!) 
       self.navigationController?.navigationItem.titleView = self.searchBarField 
       }, completion: { (success) in 
      }) 
     } 
    } 
} 
+0

Können Sie diesen Code möglicherweise aktualisieren? Ich bin mir nicht ganz sicher, ob Sie das cgRect mit dem Operanden vergleichen, danke. –

1

ich eine Swift 3 Version mit einem benutzerdefinierten "Header" implementiert und Ändern der Größe es Constraint Höhe basierend auf @ chauhan Antwort:

extension ViewController: UIScrollViewDelegate { 

    func scrollViewDidScroll(_ scrollView: UIScrollView) { 
     if scrollViewOffset < scrollView.contentOffset.y { 
      shrinkHeader(shrink: true) 
     } else if scrollViewOffset > scrollView.contentOffset.y { 
      shrinkHeader(shrink: false) 
     } 
    } 

    func shrinkHeader(shrink: Bool) { 
     if shrink { 
      if self.headerContainerHeightConstraint.constant > CGFloat(minHeaderHeight) { 
       UIView.animate(withDuration: 0.1, animations: { 
        self.headerContainerHeightConstraint.constant = self.headerContainerHeightConstraint.constant - 2 
        self.view.layoutIfNeeded() 
       }) 
      } 
     } else { 
      if self.headerContainerHeightConstraint.constant < CGFloat(maxHeaderHeight) { 
       UIView.animate(withDuration: 0.1, animations: { 
        self.headerContainerHeightConstraint.constant = self.headerContainerHeightConstraint.constant + 6 
        self.view.layoutIfNeeded() 
       }) 
      } 
     } 
    } 
}