如何根据应用程序中的某些情况以编程方式更改 UITabBarItems 的位置

How to change positions of UITabBarItems according to certain situations in the app, programmatically

我的 UITabBar 中有 3 个项目,我的应用程序运行 2 种语言(英语和阿拉伯语)。当应用程序切换到阿拉伯语时,我需要更改项目的位置,因为需要从右到左阅读

即。名为 "One Way" 的选项卡必须出现在右侧,而多城市必须出现在左侧。有办法吗?

我试过这个方法,但是不允许

 UITabBarItem *realItemOne=[self.tabBar.items objectAtIndex:0];
    UITabBarItem *realItemTwo=[self.tabBar.items objectAtIndex:1];
    UITabBarItem *realItemThree=[self.tabBar.items objectAtIndex:2];

    if (_isEnglish) {
        self.tabBar.items=[[NSArray alloc] initWithObjects:realItemOne,realItemTwo,realItemThree, nil];
    }
    else{
        self.tabBar.items=[[NSArray alloc] initWithObjects:realItemThree,realItemTwo,realItemOne, nil];
    }

也试过这个

self.tabBar.items=[[self.tabBar.items reverseObjectEnumerator] allObjects];

我试过的另一种方法

UIViewController *viewControllerOne=[self.tabBarController.viewControllers objectAtIndex:0];
    UIViewController *viewControllerTwo=[self.tabBarController.viewControllers objectAtIndex:1];
    UIViewController *viewControllerThree=[self.tabBarController.viewControllers objectAtIndex:2];
    if (_isEnglish) {
        NSArray *englishOrder=[[NSArray alloc] initWithObjects:viewControllerOne,viewControllerTwo,viewControllerThree, nil];
        [self.tabBarController setViewControllers:englishOrder];
    }
    else{
        NSArray *arabicOrder=[[NSArray alloc] initWithObjects:viewControllerThree,viewControllerTwo,viewControllerOne, nil];
        [self.tabBarController setViewControllers:arabicOrder];
    }

不要更改项目,仅更改选项卡项目的文本和图像。

if (_isEnglish) { change just image and text on 0. and 2. tab } else { also just change image and text }

当用户点击该项目时,您检查语言,然后执行适当的方法。如果用户点击 0.element 并且语言为英语,则调用 oneWay 方法,如果语言为阿拉伯语,则调用 multiCity

希望对您有所帮助。

在tabBarController中可以获取NSArray格式的controller个数,如下:

NSArray *vcArray = self.tabBarController.viewControllers;

现在根据需要更改此 "vcArray" 对象的顺序(出于演示目的,我使用了 3 个控制器。您可以随意放置逻辑)然后重新分配 "vcArray" 到您的 tabBarController,如下所示:

self.tabbarController.viewControllers = vcArray;

实际代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    FirstViewController *fvc = [[FirstViewController alloc] init];
    SecondViewController *svc = [[SecondViewController alloc] init];
    ThirdViewController *tvc = [[ThirdViewController alloc] init];

    fvc.tabBarItem.title = @"First";
    svc.tabBarItem.title = @"Second";
    tvc.tabBarItem.title = @"Third";

    _tbc = [[UITabBarController alloc] init];
    _tbc.viewControllers = [NSArray arrayWithObjects:fvc, svc, tvc, nil];

    self.window.rootViewController = _tbc;
    [self.window makeKeyAndVisible];

    return YES;
}

---------------- AppDelegate 中 tabViewController 中交换控制器的方法------------

NSArray *arr = self.tbc.viewControllers; //here self means appDelegate

self.tbc.viewControllers = [NSArray arrayWithObjects:arr[2], arr[1], arr[0], nil];

希望对您有所帮助!

附上图片供您参考。

这对我有用:

将此代码放在 TabBArController ViewWillAppear 方法中

 NSArray *actualItems= self.viewControllers;

    NSArray* reversedArray = [[actualItems reverseObjectEnumerator] allObjects];


    [self setViewControllers:reversedArray animated:YES];