【问题标题】:Set a title programmatically以编程方式设置标题
【发布时间】:2020-05-31 15:53:58
【问题描述】:

这是我的故事板。我的 TabBarController 引用了第二张图片中的 NavigationController(它有一个自己的类)。

我无法通过代码设置标题。我尝试了我在 Google & co 上找到的 MapsViewController 和 MapsNavigationViewController 中的每一段代码。

viewController.title = "一些标题"

self.title = "一些标题"

navigationBar.topItem.title = "一些标题"

self.navigationItem.title = "你的标题"

self.parent?.title = "一些标题"

navigationItem.title = "你想要什么"

如何以编程方式为我的导航设置标题...?

1)

2)

3)

编辑:查看层次结构:

【问题讨论】:

  • 您是否检查过在显示 mapViewController 时导航栏没有隐藏。 self.title = "blablabla" 应该可以调试您的视图层次结构以获得更好的想法
  • hi kjoe, self.title = "blablabla" 将更改我的 tabbarcontroller 项目的标题,但对我的 navigationcontroller 项目没有影响。将尝试调试它。编辑:导航栏当然没有隐藏
  • 但你必须在 MapViewcontroller 中而不是在 tabBarController 中进行操作,这就是为什么我建议调试视图层次结构,因为标题未显示可能导航栏因某种原因被隐藏
  • 编辑:我添加了视图层次结构编辑结束。 ——这里可能有误会。我在我的 MapViewController 中写了 self.title = "bla",而不是在 tabbarcontroller 中,它改变了 tabbaritem 标题。我试图找到如何调试视图层次结构。
  • 你有一个 TabBarController 和 NavigationController 的自定义类

标签: ios swift xcode uinavigationcontroller


【解决方案1】:

解决方法:标题颜色不能与导航栏颜色相同。感谢@kjoe。

【讨论】:

    【解决方案2】:

    在视图控制器和 tabbarController 之间插入导航控制器。将视图控制器设置为导航控制器的根视图控制器。或者你也可以嵌入。然后在 Perticular viewController 类中设置 ViewDidLoad()

    self.title = "some title"
    

    或

    self.navigationItem.title = "Your Title"
    

    【讨论】:

      【解决方案3】:

      也许下面的代码会对你有所帮助。我总是用它来以编程方式设置导航栏。

          self.navigationController?.isNavigationBarHidden = false
          self.navigationController!.navigationBar.barTintColor = UIColor.black          
          self.navigationItem.title = "Your Title"
          self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white , NSAttributedString.Key.font : UIFont.init(name: "Poppins-SemiBold", size: 15)!]
          self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
          self.navigationController?.navigationBar.shadowImage = UIImage()
          self.navigationController?.navigationBar.isTranslucent = false // its based on your requirement that your app's navigation bar should be transparent or not 
      

      您可以在 viewWillAppear 中使用此代码。

      只需在上面的代码中替换您的应用程序主题颜色和标题即可。

      【讨论】:

        猜你喜欢
        • 2011-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-08
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多