个人喜好,习惯先自定义UITabBarController,方便管理
1、创建UITabBarController的子类 RootTabBarController
class RootTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() }
2、在AppDelegate类里指定RootTabBarController为根视图
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window?.makeKeyAndVisible() let root = RootTabBarController() self.window?.rootViewController=root // Override point for customization after application launch. return true }
3、创建2个空Controller如HomeViewController、SortViewController、OtherViewController
4、在RootTabBarController类里创建tabbar的子控制器
class RootTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() //创建tabbar的子控制器 self.creatSubViewControllers() } func creatSubViewControllers(){ let firstVC = HomeViewController () let item1 : UITabBarItem = UITabBarItem (title: "第一页面", image: UIImage(named: "tabbar_home"), selectedImage: UIImage(named: "tabbar_home_selected")) firstVC.tabBarItem = item1 let secondVC = SortViewController () let item2 : UITabBarItem = UITabBarItem (title: "第二页面", image: UIImage(named: "tabbar_sort"), selectedImage: UIImage(named: "tabbar_sort_selected")) secondVC.tabBarItem = item2 let otherVC = OtherViewController () let item3 : UITabBarItem = UITabBarItem (title: "第三页面", image: UIImage(named: "tabbar_other"), selectedImage: UIImage(named: "tabbar_other_selected")) otherVC.tabBarItem = item3 let tabArray = [firstVC,secondVC,otherVC] self.viewControllers = tabArray }
运行后效果
swift-UI控件开发之UITabBarController的创建
原文:http://my.oschina.net/u/2500207/blog/524074