将选项卡栏项目添加到 UITabBar 时出现问题

发布于 2024-11-10 14:07:31 字数 514 浏览 6 评论 0原文

我有一个基于选项卡栏的应用程序,其中我尝试使用 UITabBarsetItems 方法动态地将选项卡栏项目添加到选项卡栏。

代码如下:

[self.tabBarController.tabBar setItems:self.level1TabBarItems animated:YES];

其中 self.level1TabBarItems 是一个 NSMutableArray,其中包含 4 个 UITabBarItems。 当我运行这段代码时,我从编译器中得到一个异常。

NSInternalInconsistencyException,原因:不允许直接修改由标签栏控制器管理的标签栏。

我尝试删除 UITabBarViewController 并再次添加它,但它不起作用。

I have a tab bar based application in which I am trying to add tab bar items to the tab bar dynamically using setItems method of the UITabBar.

Here is the code:

[self.tabBarController.tabBar setItems:self.level1TabBarItems animated:YES];

Where self.level1TabBarItems is an NSMutableArray with 4 UITabBarItems in it.
When I run this code, I get an exception from the compiler.

NSInternalInconsistencyException, reason:Directly modifying a tab bar managed by a tab bar controller is not allowed.

I have tried deleting the UITabBarViewController and adding it again but it did not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-11-17 14:07:31

文档明确指出您不应直接修改选项卡栏。使用 setViewControllers:animated: 代替。

The documentation clearly states that you shouldn't modify the tab bar directly. Use setViewControllers:animated: instead.

眼中杀气 2024-11-17 14:07:31

我希望以下代码可以帮助您:

func application(_application: UIApplication,
                didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = LongUITabBarController()
    window?.makeKeyAndVisible()

    return true
}
import UIKit


class LongUITabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc1 = VC1_ViewController()
        let vc2 = VC2_ViewController()
        let vc3 = VC3_ViewController()
        let vc4 = VC4_ViewController()

        vc1.tabBarItem = UITabBarItem(title: "LIST ALL", image: UIImage(named: "list"), tag: 1)
        vc2.tabBarItem = UITabBarItem(title: "BEST CELLER", image: UIImage(named: "bestCeller"), tag: 2)
        vc3.tabBarItem = UITabBarItem(title: "MOST LIKE", image: UIImage(named: "like"), tag: 3)
        vc4.tabBarItem = UITabBarItem(title: "NEW", image: UIImage(named: "new"), tag: 4)

        viewControllers = [vc1, vc2, vc3, vc4]
        setViewControllers(viewControllers, animated: true)

        // backGround for tapBarView
        tabBar.barTintColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)

    }


}

I hope can help you with following code:

func application(_application: UIApplication,
                didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = LongUITabBarController()
    window?.makeKeyAndVisible()

    return true
}
import UIKit


class LongUITabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc1 = VC1_ViewController()
        let vc2 = VC2_ViewController()
        let vc3 = VC3_ViewController()
        let vc4 = VC4_ViewController()

        vc1.tabBarItem = UITabBarItem(title: "LIST ALL", image: UIImage(named: "list"), tag: 1)
        vc2.tabBarItem = UITabBarItem(title: "BEST CELLER", image: UIImage(named: "bestCeller"), tag: 2)
        vc3.tabBarItem = UITabBarItem(title: "MOST LIKE", image: UIImage(named: "like"), tag: 3)
        vc4.tabBarItem = UITabBarItem(title: "NEW", image: UIImage(named: "new"), tag: 4)

        viewControllers = [vc1, vc2, vc3, vc4]
        setViewControllers(viewControllers, animated: true)

        // backGround for tapBarView
        tabBar.barTintColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)

    }


}
千秋岁 2024-11-17 14:07:31

AFAIK 你无法替换标签栏。这是苹果不允许的。我现在就检查一下。

不过,您可以做的是创建一个分段控制器并将其重新设计为看起来像一个选项卡栏。它的用途几乎相同。

编辑:上面,忍者海报说:你不能改变标签栏。我建议使用分段控制器。

AFAIK you can't replace the tabbar. It's not allowed by Apple. I'll check it now.

What you can do though, is creating a segmentedController and restyle it to look like a tabbar. It has pretty much the same use.

EDIT: Above, ninja poster said it: you can't alternate the tabbar. I'd suggest the segmented controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文