How to: Save order of tabs when customizing tabs in UITabBarController

后端 未结 8 1213
终归单人心
终归单人心 2020-11-29 21:02

I am having problems finding any other information than the docs for how to save the tab order for my UITabBarController, so that the user\'s customization is saved for next

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 21:28

    I'd like to share the code I have been working on for nearly 3 days now trying all sorts of combinations with many errors and failure - the general life of coding! ha ha. Anyway the code below works with Swift 5. It's the extraction of How to: Save order of tabs when customizing tabs in UITabBarController , Sam's Code, but with the for loop update for Swift 5. I have got this working great, all in the TabBarViewController.swift file.

    So if you just paste this into your Swift file, make sure your Tab Bar Items have a tag number from the Attributes Inspector and your good to go!

    Thanks again to Sam for the Code in the first place.

    import UIKit
    
    
    class TabBarController: UITabBarController, UITabBarControllerDelegate {
    
    let tabOrderKey = "customTabBarOrder"
    
    override func viewDidLoad() {
    super.viewDidLoad()
    
    self.delegate = self
    loadCustomTabOrder()
    }
    
    func loadCustomTabOrder() {
    
    let defaults = UserDefaults.standard
    let standardOrderChanged = defaults.bool(forKey: tabOrderKey)
    
    if standardOrderChanged {
        print("Standard Order has changed")
    
        var VCArray = [UIViewController]()
        var tagNumber = 0
    
        let tabBar = self as UITabBarController
    
        if let countVC = tabBar.viewControllers?.count {
            print("\(countVC) VCs in total")
            for x in 0..

提交回复
热议问题