2015-10-20 115 views
0

我在UINavigationBar上有UISegmentedControl。該UIViewController嵌入從故事情節進行UINavigationController屏幕截圖中,見下圖:iOS9:在導航欄中隱藏SegmentedControl後不會出現標題

enter image description here

如果該設備是一個iPad,我想隱藏UISegmentedControl並顯示出self.title代替。

在我viewDidLoad,我有下面的代碼:

override func viewDidLoad() { 

    if UIDevice.currentDevice().userInterfaceIdiom == .Pad { 
     segmentedControlNavBar.hidden = true 
     self.title = "Navigation Title"  

    } else { 
     segmentedControlNavBar.hidden = false 
     navigationItem.prompt = "Navigation Title" 
     self.title = nil 
    } 
} 

Resul(從模擬器):

enter image description here

我也試過viewWillAppear,但無濟於事。

如果我從Storyboard中拿出UISegmentedControlUINavigationBar標題顯示正常。

如何隱藏導航欄中的UISegmentedControl並顯示標題?

編輯

override func viewDidLoad() { 
    super.viewDidLoad() 

    segmentedControlMain.hidden = true 
    navigationItem.prompt = nil 
    title = "Navigation Title" 
    let items = self.navigationController?.navigationBar.items 
    print(items?.first?.title) 

}

日誌:可選( 「標題導航」)

我想它的存在,我該如何讓UINavigationBar的表現出來?

這些沒有工作:

self.navigationController?.view.setNeedsDisplay() 
    self.navigationController?.navigationBar.setNeedsDisplay() 

回答

1

我覺得從UINavigationBar的的topitem改變而不是被一個UILabel到一個UISegmentedControl你可能會做包含[UINavigationBar的項目]中的數據的NSLog的看他們如何訂購,導致它會呈現最上面的項目。

「導航控制器會自動創建導航欄和push和pop時,相應的導航項目」

https://developer.apple.com/library/prerelease/tvos/documentation/UIKit/Reference/UINavigationBar_Class/index.html#//apple_ref/occ/instp/UINavigationBar/items

+0

請看看我的編輯 – user1107173

+0

我想我們需要更多的信息來進行調試這件事,但如果「hidden = true」不起作用有時調用setter方法工作(不要問我爲什麼)[segmentedControlMain setHidden:YES]; 仔細檢查segmentedControlMain和視圖之間的連接,也可能會將segmentedControlMain的alpha設置更改爲0.0以使其透明 – MrJomp

+0

分段控件已隱藏。但標題沒有出現。 – user1107173