Swift3.0 创建UITabBarController

2017-06-22  本文已影响1870人  望仔超甜

效果图如下:

屏幕快照 2017-06-22 下午2.32.10.png

1.首先创建两个ViewController
HomePageViewController:
<pre>
class HomePageViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.purple
   }
}

</pre>
SecondViewController:
<pre>
class HomePageViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
   }
}

</pre>
2.创建一个RootTabBarViewController继承自UITabBarController ,在RootTabBarController类里创建tabbar的子控制器
<pre>
class RootTabBarViewController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    //创建tabbar的子控制器
    self.creatSubViewControllers()

}

func creatSubViewControllers(){
    let firstVC = HomePageViewController()
    firstVC.title = "首页"
    let navi0 = UINavigationController(rootViewController:firstVC)
    navi0.tabBarItem.title = "首页"
    navi0.tabBarItem.image = UIImage (named:"sy")
    navi0.tabBarItem.selectedImage = UIImage(named:"sy1")

    let secondVC = SecondViewController()
    secondVC.title = "我的"
    let navi1 = UINavigationController(rootViewController:secondVC)
    navi1.tabBarItem.title = "我的"
    navi1.tabBarItem.image = UIImage (named:"wd")
    navi1.tabBarItem.selectedImage = UIImage(named:"wd1")


    tabBar.tintColor = UIColor.orange
    let tabArray = [navi0,navi1]
    self.viewControllers = tabArray




}

</pre>
3.在AppDelegate类里指定RootTabBarController为根视图
<pre>
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


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

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.backgroundColor = UIColor.white
    self.window?.makeKeyAndVisible()
    let root  = RootTabBarViewController()
    self.window?.rootViewController=root

    return true
}

</pre>

上一篇下一篇

猜你喜欢

热点阅读