iOS9:在导航栏中隐藏 SegmentedControl 后,标题没有出现

标签 ios swift uinavigationcontroller ios9

我在 UINavigationBar 上有一个 UISegmentedControlUIViewController 嵌入在 UINavigationController Storyboard的屏幕截图中,如下所示:

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
    }
}

结果(来自模拟器):

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()

最佳答案

我认为 UINavigationBar 中的 topitem 已更改,而不是从 UILabel 更改为 UISegmentedControl 您可以对 [UINavigationBar items] 中包含的数据执行 NSLog 来查看它们的排序方式,因为它将呈现最上面的项目。

"The navigation controller creates the navigation bar automatically and pushes and pops navigation items when appropriate"

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

关于iOS9:在导航栏中隐藏 SegmentedControl 后,标题没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225885/

相关文章:

objective-c - 导航 Controller 问题

ios - 将 subview Controller 添加到当前 View Controller

iphone - iOS:Sqlite 的单元测试

ios - Swift 如何向函数添加编译器警告

ios - CLLocationManagerDelegate-locationManager(_ :startMonitoring:) not called when there is no network

swift - 应该改变单元配置的条件不起作用,可能是因为 feed 的填充方式

swift - 使用 Swift 以编程方式创建的 UIBarButtonItem,不可见

ios - 如何模拟返回一个未显示的ViewController

ios - 如何在 Swift 中创建不漏边框的圆形头像或圆角图片?

parse-platform - Parse 和 Swift 1.2 问题