2015-08-10 22 views
7

Sono abbastanza nuovo per lo sviluppo di iOS. In questo momento sto cercando di nascondere la mia barra delle linguette quando si scorre verso il basso e quando si scorre la barra delle schede dovrebbe apparire. Mi piacerebbe avere questo animato allo stesso modo come la barra di navigazione. Per la barra di navigazione ho semplicemente cliccato l'opzione nell'Inspector degli attributi. Ho visto alcuni esempi per la barra degli strumenti, ma non posso adottare la tabbar.iOS/Swift - Nascondi/Mostra UITabBarController durante lo scorrimento verso il basso/su

self.tabBarController?.tabBar.hidden = true nasconde semplicemente la mia barra delle linguette, ma non è animata come il controller di navigazione.

risposta

27

Questo è il codice che sto effettivamente utilizzando in un'app di produzione.

È in Swift e aggiorna anche UITabBar.hidden var.

func scrollViewWillBeginDragging(scrollView: UIScrollView) { 
    if scrollView.panGestureRecognizer.translationInView(scrollView).y < 0{ 
     changeTabBar(true, animated: true) 
    } 
    else{ 
     changeTabBar(false, animated: true) 
    } 
} 

È possibile anche usare l'altro metodo di callback:

func scrollViewDidScroll(scrollView: UIScrollView) { 
    ... 
} 

ma se si sceglie così, allora si più gestire più chiamate al metodo di supporto che nasconde in realtà la barra delle linguette.

E quindi è necessario aggiungere questo metodo che anima il nascondi/mostra del tabBar.

func changeTabBar(hidden:Bool, animated: Bool){ 
    var tabBar = self.tabBarController?.tabBar 
    if tabBar!.hidden == hidden{ return } 
    let frame = tabBar?.frame 
    let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!) 
    let duration:NSTimeInterval = (animated ? 0.5 : 0.0) 
    tabBar?.hidden = false 
    if frame != nil 
    { 
     UIView.animateWithDuration(duration, 
      animations: {tabBar!.frame = CGRectOffset(frame!, 0, offset)}, 
      completion: { 
       println($0) 
       if $0 {tabBar?.hidden = hidden} 
     }) 
    } 
} 
+0

Grazie per la tua risposta, proverò ad implementarlo più avanti nel mio progetto –

+0

Funziona alla grande. Questo e 'esattamente quello che stavo cercando. Grazie. –

+0

Ottima risposta. Grazie –

0

È possibile controllare UITabBar in modo preciso impostando la classe come delegato per scrollView e implementando lo scorrimento nel metodo scrollViewDidScroll:.

Ecco un esempio di come faccio la mia applicazione. Probabilmente puoi facilmente modificarlo per le tue esigenze. Alcune funzioni di supporto per includere UITabBar.

#define LIMIT(__VALUE__, __MIN__, __MAX__) MAX(__MIN__, MIN(__MAX__, __VALUE__)) 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    CGFloat scrollOffset = scrollView.contentOffset.y; 
    CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset; 
    CGFloat scrollHeight = scrollView.frame.size.height; 
    CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom; 
    CGFloat scrollOffsetGlobal = scrollOffset + scrollView.contentInset.top; 
    [self updateUITabBarY:[self UITabBarView].frame.origin.y + scrollDiff]; 
    self.previousScrollViewYOffset = scrollOffset; 
} 

- (UITabBar*) UITabBarView 
{ 
    for(UIView *view in self.tabBarController.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      return (UITabBar*) view; 
     } 
    } 

    return nil; 
} 

- (void) updateUITabBarY:(CGFloat) y 
{ 
    UITabBar* tabBar = [self UITabBarView]; 
    if(tabBar) 
    { 
     CGRect frame = tabBar.frame; 
     frame.origin.y = LIMIT(y, [self UITabBarMiny], [self UITabBarMaxY]); 
     tabBar.frame = frame; 
    } 
} 

- (CGFloat) UITabBarMiny 
{ 
    return [UIScreen mainScreen].bounds.size.height - [self UITabBarView].frame.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height + 20.0f; 
} 

- (CGFloat) UITabBarMaxY 
{ 
    return [UIScreen mainScreen].bounds.size.height; 
} 
+0

Grazie per la risposta. Proverò prima la risposta rapida. –

+0

Che cos'è il LIMIT nel codice? – Apan

+0

Sry Ho aggiunto la macro mancante per te - limita solo il primo parametro ad un certo intervallo - (valore, MIN, MAX) –

4

Sulla risposta di Ariel, ho aggiornato il codice per Swift3. Questo ha funzionato alla grande sulle mie viste di collezione.

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { 
     if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 { 
      changeTabBar(hidden: true, animated: true) 
     }else{ 
      changeTabBar(hidden: false, animated: true) 
     } 

    } 

func changeTabBar(hidden:Bool, animated: Bool){ 
     let tabBar = self.tabBarController?.tabBar 
     if tabBar!.isHidden == hidden{ return } 
     let frame = tabBar?.frame 
     let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!) 
     let duration:TimeInterval = (animated ? 0.5 : 0.0) 
     tabBar?.isHidden = false 
     if frame != nil 
     { 
      UIView.animate(withDuration: duration, 
             animations: {tabBar!.frame = frame!.offsetBy(dx: 0, dy: offset)}, 
             completion: { 
             print($0) 
             if $0 {tabBar?.isHidden = hidden} 
      }) 
     } 
    } 
2

Questa risposta è una leggera modifica al Ariel risposta che aggiunge animazione mentre utente scorre.

extension ViewController:UIScrollViewDelegate{ 
    func scrollViewDidScroll(_ scrollView: UIScrollView) { 
     if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{ 
      //scrolling down 
      changeTabBar(hidden: true, animated: true) 
     } 
     else{ 
      //scrolling up 
      changeTabBar(hidden: false, animated: true) 
     } 
    } 

    func changeTabBar(hidden:Bool, animated: Bool){ 
     let tabBar = self.tabBarController?.tabBar 
     let offset = (hidden ? UIScreen.main.bounds.size.height : UIScreen.main.bounds.size.height - (tabBar?.frame.size.height)!) 
     if offset == tabBar?.frame.origin.y {return} 
     print("changing origin y position") 
     let duration:TimeInterval = (animated ? 0.5 : 0.0) 
     UIView.animate(withDuration: duration, 
         animations: {tabBar!.frame.origin.y = offset}, 
         completion:nil) 
    } 
} 
Problemi correlati