美文网首页iOS学习
Swift初学 创建TabBarViewController

Swift初学 创建TabBarViewController

作者: 慌莫染 | 来源:发表于2018-03-06 16:15 被阅读11次

1.先创建一个MainTabBarVC 继承 UITabBarController
2.AppDelegate.swift 文件代码

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let rootVC = MainTabBarVC()
        self.window?.rootViewController = rootVC
        self.window!.makeKeyAndVisible()
        return true
    }

3.TabBarViewController.swift 代码

class MainTabBarVC: UITabBarController {

    var _backView:UIView? = nil
    var items:NSArray = []
    let nameArr = ["游戏","开服","礼包","我的"]
    let picArr = ["game","ic_home_normal","gift","mine"]
    let picSelectArr = ["gameSelected","ic_home_checked","giftSelected","mineSelected"]
    
    var navVCArr:[NSObject] = [NSObject]()
    var nav:UINavigationController = UINavigationController()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.CreatTabBar()
        
    }

    func CreatTabBar() {
        _backView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 49))
        
        for M in 0..<nameArr.count {
            let mainVC = ViewController()
            
            nav = UINavigationController(rootViewController: mainVC)
            nav.tabBarItem.title = nameArr[M]
            nav.tabBarItem.image = UIImage(named: picArr[M])
            nav.tabBarItem.selectedImage = UIImage(named: picSelectArr[M])
            mainVC.title = nameArr[M]
            navVCArr.append(nav)
        }
        self.viewControllers = navVCArr as? [UIViewController]
//        for  i in 0 ..< navVCArr.count {
//            //设置导航栏的背景图片 (优先级高)
//            (navVCArr[i] as AnyObject).navigationBar.setBackgroundImage(UIImage(named:"NavigationBar"), for:.default)
//            //设置导航栏的背景颜色 (优先级低)
//            (navVCArr[i] as AnyObject).navigationBar.barTintColor = UIColor.orange
//            //设置导航栏的字体颜色
////            (navVCArr[i] as AnyObject).navigationBar.titleTextAttributes =
////                [NSForegroundColorAttributeName: UIColor.red]
//
//        }
            
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
感谢 图片.png

相关文章

网友评论

    本文标题:Swift初学 创建TabBarViewController

    本文链接:https://www.haomeiwen.com/subject/mcehfftx.html