启用禁用的标签

时间:2018-09-16 08:50:32

标签: ios swift

我有一个UITabBar,其中包含5个标签。我禁用了以下选项卡之一:

tabBar.items?[3].isEnabled = false

要再次启用它,我正在使用以下代码:

tabBar.items?[3].isEnabled = true

问题是实际上并没有再次启用它。我还尝试将上面的代码放在viewWillAppearviewDidAppear中,但该选项卡保持禁用状态。

这是完整的代码:

import UIKit

class MainTabViewController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    tabBar.items?[0].title = NSLocalizedString("tab1", comment: "-")
    tabBar.items?[1].title = NSLocalizedString("tab2", comment: "-")
    tabBar.items?[2].title = NSLocalizedString("tab3", comment: "-")
    tabBar.items?[3].title = NSLocalizedString("tab4", comment: "-")
    tabBar.items?[4].title = NSLocalizedString("tab5", comment: "-")

    self.tabBar.items?[3].isEnabled = true

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

}

}

1 个答案:

答案 0 :(得分:0)

它应该是tabBar的{​​{1}}属性。

UITabBarController

如果从自定义self.tabBarController?.tabBar.items?[3].isEnabled = false 子类内部调用它:

UITabBarController

请确保自定义self.tabBar.items?[3].isEnabled = false 子类的viewWillAppearviewDidAppear仅被调用一次,这与每个选项卡中的方法不同,因为每次选择选项卡时都会调用它们。

以下代码似乎可以正常工作:

UITabBarController