【问题标题】:Swift: Setting Different Navigation Bar Buttons for Different TabsSwift:为不同的选项卡设置不同的导航栏按钮
【发布时间】:2017-03-19 19:19:21
【问题描述】:

我在 Swift 3.0 中创建了一个带有 5 个选项卡的自定义 UITabBarController。我一直在尝试为某些选项卡设置不同的导航栏项目,但效果不佳。

情况:我在每个选项卡的 viewDidLoad() 中放置了可以更改导航栏按钮的代码。

//The customized Tab Bar controller instance. Contained in each of the tabs.
var mainController: TabBarController?

override func viewDidLoad() {
    // ... more code
    setupNavBar()
    // ... more code
}

func setupNavBar() {
    // ... more code
    mainController?.navigationItem.leftBarButtonItem = UIBarButtonItem(image: friendsImage, style: .plain, target: self, action: #selector(handleFindFriends))
    // ... more code
}

问题:假设 Tab #1 应该有 NavBarButton A,而 Tab #2 应该有 NavBarButton B。 当我从 Tab #1 切换到 Tab #2 时,代码工作正常; NavBarButton 从 A 变为 B。 但是,当我单击 Tab #1 时,NavBarButton 仍然是 B。

我怎样才能使导航栏按钮即使在我单击之前所在的选项卡时也会相应地发生变化?

【问题讨论】:

  • 让我们为每个 Tab 创建一个 TabBarController 和 (NavigationController > ViewController)。所以你可以为每个 ViewController 添加 barButtonItem。
  • 我已将代码添加到我的答案中,请看一下。

标签: ios swift tabs navigationbar tabbar


【解决方案1】:

我假设您将 UITabBarController(或其子类)嵌入到 UIViewController 中。这是错误的,因为在这种情况下,您倾向于使视图控制器通用,这通常是一种不好的做法。

相反,我建议将视图控制器的层次结构更改为您在下图中看到的内容。

更新

如果你在代码中这样做:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let tabBarController = UITabBarController()

        // first tab
        let firstViewController = UIViewController()
        _ = firstViewController.view
        firstViewController.title = "First"
        let firstNavigationController = UINavigationController(rootViewController: firstViewController)

        // sets a specific button ("Friends") on a navigation bar for the first screen
        firstViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Friends", style: .plain, target: nil, action: nil)

        // second tab
        let secondViewController = UIViewController()
        _ = secondViewController.view
        secondViewController.title = "Second"
        let secondNavigationController = UINavigationController(rootViewController: secondViewController)

        // sets a specific button ("Photos") on a navigation bar for the second screen
        secondViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Photos", style: .plain, target: nil, action: nil)

        // embeds the navigation controllers into the tab bar controller
        tabBarController.viewControllers = [firstNavigationController, secondNavigationController]

        // creates a window with the tab bar controller inside
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = tabBarController
        window?.makeKeyAndVisible()

        return true
    }

}

【讨论】:

  • 这就是我的回答,干得好@Artem Stepanenko
【解决方案2】:

其实我找到了解决办法 XD 在 TabBarController 中:

override func viewWillAppear(_ animated: Bool) {
    //Original code to set up tabs

    //Code I added:
    for i in 0...4 {
        tabBar.items?[i].tag = i
    }
}
//Code I added:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    //Code to run when a certain tab is selected
}

仍然非常感谢!

【讨论】:

    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2019-07-17
    • 2018-07-19
    • 1970-01-01
    相关资源
    最近更新 更多