创建UITabBarController(Swift3.0)

1.首先创建两个ViewController

HomeViewController 和 PageViewController

2.创建一个TabBarViewController继承自UITabBarController ,在TabBarController类里创建tabbar的子控制器
class TabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        creatSubViewController()
        
    }
    func creatSubViewController() {
        let homeVC = HomeViewController()
        homeVC.title = "首页"
        let nav = UINavigationController(rootViewController: homeVC)
        nav.tabBarItem.image = UIImage(named: "weidianji")
        nav.tabBarItem.selectedImage = UIImage(named: "zhuye")
        
        let pageVC = PageViewController()
        pageVC.title = "我的"
        pageVC.tabBarItem.image = UIImage(named: "wode-1")
        pageVC.tabBarItem.selectedImage = UIImage(named: "wode")
        
        let tabArr = [nav,pageVC]
        viewControllers = tabArr
    }
}
3.在AppDelegate类里指定TabBarController为根视图
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.main.bounds)
        let vc = TabBarViewController()
        window?.rootViewController = vc
        window?.makeKeyAndVisible()
        
        return true
    }
效果图如下:
创建UITabBarController(Swift3.0)_第1张图片
Simulator Screen Shot - iPhone 8 Plus - 2017-11-28 at 10.17.32.png

你可能感兴趣的:(创建UITabBarController(Swift3.0))