一.修改项目的启动过程

  • 将Main Interface处的main删除
  • 在application:didFinishLaunchingWithOptions:launchOptions:方法中创建window,并且设置根控制器
    // 设置整体主题TabBar的tintColor
    UITabBar.appearance().tintColor = UIColor.orangeColor()

    // 1.创建window
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.backgroundColor = UIColor.whiteColor()

    // 2.设置window的根控制器
    self.window?.rootViewController = MainViewController()

    // 3.让窗口生效
    self.window?.makeKeyAndVisible()
  • 在MainViewController中添加子控制器
    override func viewDidLoad() {
        super.viewDidLoad()

        // 添加自控制器
        self.addChildViewController(HomeViewController(), imageName: "tabbar_home", title: "主页")
        self.addChildViewController(MessageViewController(), imageName: "tabbar_message_center", title: "消息")
        self.addChildViewController(DiscoverViewController(), imageName: "tabbar_discover", title: "广场")
        self.addChildViewController(ProfileViewController(), imageName: "tabbar_profile", title: "我")
    }

    private func addChildViewController(childCVc: UIViewController, imageName : String, title : String) {
        // 1.创建自控制器
        let homeNav = UINavigationController(rootViewController: childCVc)

        // 2.设置标题
        childCVc.title = title
        childCVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        childCVc.tabBarItem.image = UIImage(named: imageName)

        // 3.添加到UITabbarController
        self.addChildViewController(homeNav)
    }

相关文章:

  • 2021-12-05
  • 2022-01-01
  • 2021-04-19
  • 2021-06-22
  • 2021-10-20
  • 2021-09-03
  • 2021-08-26
  • 2021-10-18
猜你喜欢
  • 2021-05-25
  • 2021-07-08
  • 2021-07-12
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案