iOS - 以编程方式将 SegmentControl 替换为导航标题

iOS - Replace SegmentControl with navigation title programmatically

我有一个视图控制器,它的导航栏有一个段控件。现在我想重用这个视图控制器来显示带有标题而不是段控件的其他内容。我试图将段控件的隐藏属性设置为true并设置self.navigationController?.title = "foo",不幸的是,没有显示标题:(

如何以编程方式将 UISegmentedControl 替换为我的导航标题?

您必须将 navigationControllertitleView property 设置为 nil,因为文档声明

If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when the receiver is the top item. If you set this property to a custom title, it is displayed instead of the title. This property is ignored if leftBarButtonItem is not nil.

可以这样替换titleView中的UISegmentedView

self.navigationItem.titleView = nil
self.navigationItem.title = "Title"