以编程方式设置标题

时间:2020-02-16 15:44:09

标签: ios swift xcode uinavigationcontroller

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

我无法通过代码设置标题。我在Google&co。找到的 MapsViewController MapsNavigationViewController 中尝试了所有代码。

viewController.title =“某些标题” <-不起作用

self.title =“某些标题” <-不起作用

navigationBar.topItem.title =“某些标题” <-不起作用

self.navigationItem.title =“您的标题” <-不起作用

self.parent?.title =“某些标题” <-不起作用

navigationItem.title =“你想要什么” <-不起作用

如何通过编程方式为导航设置标题??

1)enter image description here

2)enter image description here

3)enter image description here

编辑:查看层次结构:

enter image description here

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

self.title = "some title"

OR

self.navigationItem.title = "Your Title"

答案 2 :(得分:0)

也许下面的代码会为您提供帮助。我一直在以编程方式将其用于设置导航栏。

    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中使用此代码。

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

相关问题