我在 UINavigationBar
上有一个 UISegmentedControl
。 UIViewController
嵌入在 UINavigationController
Storyboard的屏幕截图中,如下所示:
如果设备是 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
}
}
结果(来自模拟器):
我也尝试了 viewWillAppear
,但没有用。
如果我从 Storyboard
中取出 UISegmentedControl
,UINavigationBar
标题显示正常。
如何从导航栏中隐藏 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()
最佳答案
我认为 UINavigationBar 中的 topitem 已更改,而不是从 UILabel 更改为 UISegmentedControl 您可以对 [UINavigationBar items] 中包含的数据执行 NSLog 来查看它们的排序方式,因为它将呈现最上面的项目。
"The navigation controller creates the navigation bar automatically and pushes and pops navigation items when appropriate"
关于iOS9:在导航栏中隐藏 SegmentedControl 后,标题没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225885/