简体   繁体   中英

Change order of tab bar items

I'm having a view which has a tab bar at the bottom. Now I want to change the items of the tab bar based on iPhone and iPad (ie different orders for iPhone and iPad).

In viewDidLoad if I do something like self.viewControllers.count , I get the proper count of tab bars. But how to proceed from there in order to change the order of tab bar items I can't figure out. I mean how can I get the index of each of the viewcontrollers so that I can rearrange their indexes I can't figure out...

You can define your tab bar programmatically. but first, you should set an id for your tab views in storyboard like in image: 为视图设置ID

in AppDelegate , didFinishLaunchingWithOptions method:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
// change "Main" if your storyboard's filename is different.

let vc1 = storyboard.instantiateViewController(withIdentifier: "vc1")
let vc2 = storyboard.instantiateViewController(withIdentifier: "vc2")

let tab = UITabBarController()
if UIDevice.current.model.lowercased() == "iphone" {
    tab.viewControllers = [vc1, vc2]
} else {
    tab.viewControllers = [vc2, vc1]
}

window?.rootViewController = tab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM