简体   繁体   English

kukushi/SideMenu 导航标题不变

[英]kukushi/SideMenu navigation title not changing

I am using SideMenuSwift by kukushi.我正在使用 kukushi 的 SideMenuSwift。

I create a SideMenuController programmitcally with following code:我使用以下代码以编程方式创建一个 SideMenuController:

extension DeviceScanViewController {
  static func instantiate() -> UIViewController {
    
    let homeViewController = UIStoryboard(name: "DeviceScan", bundle: nil).instantiateInitialViewController()!.embedInNavigationController()
    let menuVC = MenuViewController.instantiate()
    return SideMenuController(contentViewController: homeViewController, menuViewController: menuVC)
  }
}

The navigation bar title shows the name of the app like in this image ("Appname"):导航栏标题显示此图像中的应用程序名称(“Appname”):

在此处输入图像描述

I would like to change the navigation title to something else but I don't know how.我想将导航标题更改为其他名称,但我不知道如何更改。

I have tried following ways:我尝试了以下方法:

  override func viewDidLoad() {
    super.viewDidLoad()
    
    self.title = "Test123"
    self.navigationController?.navigationBar.topItem?.title = "Test456"
    self.navigationItem.title = "Test789"
    
    sideMenuController?.title = "Test123"
    sideMenuController?.navigationController?.navigationBar.topItem?.title = "Test456"
    sideMenuController?.navigationItem.title = "Test789"
}

None of these ways work.这些方法都不起作用。 The navigation title still shows the app name.导航标题仍然显示应用名称。 How can I change the navigation bar title when using SideMenuSwift?使用 SideMenuSwift 时如何更改导航栏标题?

I found out that the title of the navigation bar was being set in a function inside an extension of UIViewController called reachabilityChanged.我发现导航栏的标题是在名为 reachabilityChanged 的 UIViewController 扩展内的一个函数中设置的。 This function was called automatically whenever the connection status of the app was updated.每当更新应用程序的连接状态时,都会自动调用此函数。 Therefore every time some views were loaded it would override the navigation bar title with "Appname" or "Appname (offline)".因此,每次加载某些视图时,它都会使用“Appname”或“Appname(离线)”覆盖导航栏标题。 So it has nothing to do with the SideMenu by kukushi.所以它与 kukushi 的 SideMenu 无关。

  func reachabilityChanged(_ isConnected: Bool) {
    let navBarColor: UIColor = isConnected ? .white : .offlineOrange
    let navBarTextColor: UIColor = isConnected ? .defaultPurple : .white
    
    updateViewStyle(backgroundColor: .white, navBarColor: navBarColor, navBarTextColor: navBarTextColor)
     //TODO: Localize
      title = isConnected ? "Appname".localized : "Appname (offline)".localized
  }

I managed to solve it by adding the following code to the view controllers and setting a tag for which I would needed to set title to "Appname" or "Appname (offline)":我设法通过将以下代码添加到视图控制器并设置一个标签来解决它,我需要为其设置标题为“Appname”或“Appname(离线)”:

  override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set view tag 111 so that in UIViewController extension function
    // reachabilityChanged Appname/Appname (offline) navigation title is set
    // for this view and not for Settings
    self.view.tag = 111
}

and for the second view controller as well:以及第二个视图控制器:

  override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set view tag 112 so that in UIViewController extension function
    // reachabilityChanged Appname/Appname (offline) navigation title is set
    // for this view and not for Settings
    self.view.tag = 112

}

And then I changed the function reachabilityChanged to the following:然后我将函数 reachabilityChanged 更改为以下内容:

  func reachabilityChanged(_ isConnected: Bool) {
    let navBarColor: UIColor = isConnected ? .white : .offlineOrange
    let navBarTextColor: UIColor = isConnected ? .defaultPurple : .white
    
    updateViewStyle(backgroundColor: .white, navBarColor: navBarColor, navBarTextColor: navBarTextColor)
     //TODO: Localize
    // view tags 111 and 112 are Device Scan view and Manual Scan view
    if(self.view.tag == 111 || self.view.tag == 112){
      title = isConnected ? "Appname".localized : "Appname (offline)".localized
    }
  }

Now it works if I set the self.title to another text in a different view controller.现在,如果我将 self.title 设置为不同视图控制器中的另一个文本,它就可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 kukushi/SideMenu 显示菜单无法从呈现的 anotherViewController 工作 - kukushi/SideMenu Reveal Menu not working from presented anotherViewController 在MFMailComposeViewController中更改导航栏标题 - Changing navigation bar title in MFMailComposeViewController 在Interface Builder中更改导航栏标题字体 - Changing Navigation Bar Title Font in Interface Builder 导航栏显示在侧边菜单导航iOS Xamarin - Navigation bar showing on sidemenu navigation ios xamarin 导航栏在更改为内联之前作为大标题短暂闪烁 - Navigation bar briefly flickering as large title before changing to inline 向后发送导航栏,向前发送我的侧面菜单 - Send navigation bar to back and my sidemenu to front 带有导航栏的Sidemenu仅推送内容 - Sidemenu with navigation bar that push content only 在Xcode中更改导航栏的高度后如何更改标题的位置 - How to change the postion of the title after changing the height of Navigation Bar in Xcode 导航栏标题-更改字体而不更改大小 - Navigation Bar Title - Change Font without changing size 当“prefersLargeTitles”设置为true时,更改导航栏标题的文本颜色 - Changing the text color of a navigation bar title when “prefersLargeTitles” is set to true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM