2014-10-04 81 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 
}