无法按要求的顺序显示标签栏图标

时间:2019-02-15 08:11:34

标签: ios swift uitabbarcontroller uitabbar

我想按特定顺序在标签栏上显示6个图标。为此,我在情节提要中拖动了一个选项卡控制器,并向其中添加了6个视图控制器。我还向每个选项卡栏添加了图像,并为每个选项卡栏制作了不同的视图控制器。

但是问题是图像/视图控制器未按照情节提要中指定的顺序显示。

我还有一个.swift文件用于tabbarcontroller。它具有一些用于配置标签栏的代码,如下所示...

class SampleTabBarController: UITabBarController, UIGestureRecognizerDelegate {

  // MARK: - Shared Instance
  static var shared: SampleTabBarController?

  // MARK: - Public Properties
  var homeTitle = "Home"
  var connectionsTitle = "Connections"
  var flightTitle = "Flights"
  var messagesTitle = "Messages"
  var companyProfileTitle = "Company Profile"
  var myCalendarTitle = "My Calendar"

  // MARK: - Private Properties
  fileprivate var homeNavigation = "HomeNavigation"
  fileprivate var home: HomeViewController? = nil
  fileprivate var homeIndex = 0

  fileprivate var connectionsNavigation = "ConnectionsNavigation"
  fileprivate var connections: ConnectionsViewController? = nil
  fileprivate var connectionsIndex = 1

  fileprivate var flightsNavigation = "FlightsNavigation"
  fileprivate var flights: FlightsViewController? = nil
  fileprivate var flightsIndex = 2

  fileprivate var messagesNavigation = "MessagesNavigation"
  fileprivate var messages: MessagesViewController? = nil
  fileprivate var messagesIndex = 3

  fileprivate var companyProfileNavigation = "CompanyProfileNavigation"
  fileprivate var compProfile: CompanyProfileViewController? = nil
  fileprivate var compProfileIndex = 4

  fileprivate var myCalendarNavigation = "MyCalendarNavigation"
  fileprivate var myCalendar: MyCalendarViewController? = nil
  fileprivate var myCalendarIndex = 5

  fileprivate var previousIndex = 0

  override func viewDidLoad() {
    super.viewDidLoad()

    SampleTabBarController.shared = self

    initialConfiguration()

  }
}

extension SampleTabBarController {

  private func initialConfiguration() {

    self.tabBarController(SampleTabBarController.shared!,
                          didEndCustomizing: SampleTabBarController.shared!.viewControllers!,
                          changed: true)


    configureViewControllers()
  }

  fileprivate func configureViewControllers() {

    // Home
    if let viewControllers = self.viewControllers {
      let navigationControllers = viewControllers.filter { $0.restorationIdentifier == homeNavigation }
      if let navigation = navigationControllers.first as? UINavigationController ,
        let homeView = navigation.viewControllers[0] as? HomeViewController {
        self.home = homeView
      }
    }
    // Connections
    if let viewControllers = self.viewControllers {
      let navigationControllers = viewControllers.filter { $0.restorationIdentifier == connectionsNavigation }
      if let navigation = navigationControllers.first as? UINavigationController ,
        let connectionView = navigation.viewControllers[0] as? ConnectionsViewController {
        self.connections = connectionView
      }
    }
    // Flight
    if let viewControllers = self.viewControllers {
      let navigationControllers = viewControllers.filter { $0.restorationIdentifier == flightsNavigation }
      if let navigation = navigationControllers.first as? UINavigationController ,
        let flightView = navigation.viewControllers[0] as? FlightsViewController {
        self.flights = flightView
      }
    }
    // Messages
    if let viewControllers = self.viewControllers {
      let navigationControllers = viewControllers.filter { $0.restorationIdentifier == messagesNavigation }
      if let navigation = navigationControllers.first as? UINavigationController ,
        let messagesView = navigation.viewControllers[0] as? MessagesViewController {
        self.messages = messagesView
      }
    }
    // Company Profile
    if let viewControllers = self.viewControllers {
      let navigationControllers = viewControllers.filter { $0.restorationIdentifier == companyProfileNavigation }
      if let navigation = navigationControllers.first as? UINavigationController ,
        let companyProfileView = navigation.viewControllers[0] as? CompanyProfileViewController {
        self.compProfile = companyProfileView
      }
    }
    // My Calendar
    if let viewControllers = self.viewControllers {
      let navigationControllers = viewControllers.filter { $0.restorationIdentifier == myCalendarNavigation }
      if let navigation = navigationControllers.first as? UINavigationController ,
        let calendarView = navigation.viewControllers[0] as? MyCalendarViewController {
        self.myCalendar = calendarView
      }
    }
  }
}

// MARK: - UITabBarControllerDelegate
extension SampleTabBarController: UITabBarControllerDelegate {

  func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    if previousIndex != tabBarController.selectedIndex {
      previousIndex = tabBarController.selectedIndex
      var elementText = ""
      var elementValue = ""

      switch previousIndex {
      case homeIndex:
        elementText = homeTitle
        elementValue = BottomBarItems.home.rawValue
      case connectionsIndex:
        elementText = connectionsTitle
        elementValue = BottomBarItems.connections.rawValue
      case flightsIndex:
        elementText = flightTitle
        elementValue = BottomBarItems.flight.rawValue
      case messagesIndex:
        elementText = messagesTitle
        elementValue = BottomBarItems.messages.rawValue
      case compProfileIndex:
        elementText = companyProfileTitle
        elementValue = BottomBarItems.companyProfile.rawValue
      case myCalendarIndex:
        elementText = myCalendarTitle
        elementValue = BottomBarItems.myCalendar.rawValue
      default:
        break
      }
    }
  }

  func tabBarController(_ tabBarController: UITabBarController, didEndCustomizing viewControllers: [UIViewController], changed: Bool) {
    for (index, viewcontroller) in viewControllers.enumerated() {
      // Home
      if viewcontroller.restorationIdentifier == homeNavigation {
        homeIndex = index
      }
      // Connections
      if viewcontroller.restorationIdentifier == connectionsNavigation {
        connectionsIndex = index
      }
      // Flight
      if viewcontroller.restorationIdentifier == flightsNavigation {
        flightsIndex = index
      }
      // Messages
      if viewcontroller.restorationIdentifier == messagesNavigation {
        messagesIndex = index
      }
      // Company Profile
      if viewcontroller.restorationIdentifier == companyProfileNavigation {
        compProfileIndex = index
      }
      // My Calendar
      if viewcontroller.restorationIdentifier == myCalendarNavigation {
        myCalendarIndex = index
      }
    }
  }

}

但这似乎不起作用...我做错了什么...?

0 个答案:

没有答案