2014-10-04 80 views
19

iOS 8增加了一个超酷的新功能:当用户滚动时隐藏导航栏。滚动时隐藏状态栏

这与viewDidload一行:

navigationController?.hidesBarsOnSwipe = true 

酷,不是吗?

但现在我有一个小问题:当导航栏被隐藏,状态栏还在这里和重复的内容,这是丑陋的。

导航栏隐藏时应该如何隐藏它?

+0

哎,你有没有找到如何做到这一点?谢谢! – dot 2014-11-19 00:58:02

+0

没有不幸...你能加+1吗? – jmcastel 2014-11-19 09:02:13

+0

看看我的问题:http://stackoverflow.com/questions/25870382/how-to-prevent-status-bar-from-overlapping-content-with-hidesbarsonswipe-set-on – 2014-11-24 20:52:58

回答

0

我立足于this post一些意见,这是猜测这个答案。我不确定这是否会起作用,因为Apple在导航栏隐藏时没有给我们任何直接的方式或委托方法。

UINavigationBar的子类作为NavigationBar。属性观测添加到其hidden财产,像这样:

var hidden: Bool{ 
didSet{ 
    UIApplication.sharedApplication().setStatusBarHidden(self.hidden, animation: .Slide) 
} 
} 

你想,然后去你viewDidLoad方法在您的主视图控制器,并设置self.navigationBar属性(或self.navigationController.navigationBar,不知道哪一个),以一个实例新的NavigationBar类。

请注意,我现在不能测试此权利,让我知道如何/如果这个工程。

+0

它没有。我必须在哪里放置var隐藏声明? self.navigationController.navigationBar给我“UINavigationController没有名为navigationBar的成员”。 self.navigationBar给我“myTableViewController没有名为navigationBar的成员” – jmcastel 2014-10-07 10:12:51

+0

@jmcastel不是UINavigationController有导航栏吗? https://developer.apple.com/library/ios/documentation/Uikit/reference/UINavigationController_Class/index.html#//apple_ref/occ/instp/UINavigationController/navigationBar – erdekhayser 2014-10-07 12:22:48

+0

我知道,但那是错误;( – jmcastel 2014-10-07 12:59:08

1

这是固定的问题在Xcode 6.1

navigationController .navigationBar.hidden =真

+1

我只是当导航栏被隐藏时想隐藏状态栏,如何监控? – jmcastel 2014-10-24 08:20:34

10

覆盖上的UIViewController以下方法:

extension MyViewController { 
    override func prefersStatusBarHidden() -> Bool { 
    return barsHidden // this is a custom property 
    } 

    // Override only if you want a different animation than the default 
    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation { 
    return .Slide 
    } 
} 

更新barsHidden在某处代码和电话 setNeedsStatusBarAppearanceUpdate()

0

您可以使用UISwipeGestureRecognizer检测滑动。我使用它的UIWebView:

在viewDidLoad中我有:

let swipeUp = UISwipeGestureRecognizer(target: self, action: "didSwipe") 
let swipeDown = UISwipeGestureRecognizer(target: self, action: "didSwipe") 
swipeUp.direction = UISwipeGestureRecognizerDirection.Up 
swipeDown.direction = UISwipeGestureRecognizerDirection.Down 
webView.addGestureRecognizer(swipeUp) 
webView.addGestureRecognizer(swipeDown) 
navigationController?.hidesBarsOnSwipe = true 

我也有一个扩展我的ViewController,叫WebViewViewController:

extension WebViewViewController { 
    override func prefersStatusBarHidden() -> Bool { 
     return hideStatusBar 
    } 

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation { 
     return UIStatusBarAnimation.Slide 
    } 
} 

在我WebViewViewController一个一流水平我也有:

var hideStatusBar = false 

func didSwipe() { 
    hideStatusBar = true 
} 
0

好吧我花了一整天这样做,希望这个hel ps有人出去了。有一个barHideOnSwipeGestureRecognizer。所以你可以为相应的UIPanGesture做一个监听器,注意如果导航栏是隐藏的,那么它的y原点是-44.0;否则,它是0(不是20,因为我们隐藏了状态栏!)。

在您的视图控制器:

// Declare at beginning 
var curFramePosition: Double! 
var showStatusBar: Bool = true 
self.navigationController?.barHideOnSwipeGestureRecognizer.addTarget(self, action: "didSwipe:") 

... 

override func viewDidLoad(){ 
    self.navigationController?.hidesBarsOnSwipe = true 
    curFramePosition = 0.0 // Not hidden 
    self.navigationController?.barHideOnSwipeGestureRecognizer.addTarget(self, action: "didSwipe:") 
    ... 
} 

func didSwipe(swipe: UIPanGestureRecognizer){ 
    // Visible to hidden 
    if curFramePosition == 0 && self.navigationController?.navigationBar.frame.origin.y == -44 { 
     curFramePosition = -44 
     showStatusBar = false 
     prefersStatusBarHidden() 
     setNeedsStatusBarAppearanceUpdate() 
    } 
    // Hidden to visible 
    else if curFramePosition == -44 && self.navigationController?.navigationBar.frame.origin.y == 0 { 
     curFramePosition = 0 
     showStatusBar = true 
     prefersStatusBarHidden() 
     setNeedsStatusBarAppearanceUpdate() 
    } 
} 

override func prefersStatusBarHidden() -> Bool { 
    if showStatusBar{ 
     return false 
    } 
    return true 
}