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

Ios 如何根据应用程序中的特定情况以编程方式更改UITabBarItems的位置,ios,objective-c,uitabbar,Ios,Objective C,Uitabbar,我的UITabBar中有3个项目,我的应用程序运行两种语言(英语和阿拉伯语)。我需要改变项目的位置时,应用程序切换到阿拉伯语,因为从右到左阅读是必需的 即,“单向”选项卡必须显示在右侧,多城市选项卡必须显示在左侧。有办法吗 我试过这种方法,但那是不允许的 UITabBarItem *realItemOne=[self.tabBar.items objectAtIndex:0]; UITabBarItem *realItemTwo=[self.tabBar.items objectAtI

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

即,“单向”选项卡必须显示在右侧,多城市选项卡必须显示在左侧。有办法吗

我试过这种方法,但那是不允许的

 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。元素,语言为英语,则称为单向方法,如果语言为阿拉伯语,则称为多城市


我希望这会有所帮助。

您可以获得以下NSArray格式的tabBarController中的控制器数量:

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中控制器的方法-----------

希望这有帮助

附件图片供您参考

这对我很有用:

将此代码放在TabBArController ViewWillAspect方法中

 NSArray *actualItems= self.viewControllers;

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


    [self setViewControllers:reversedArray animated:YES];

我试过这个的扩展版本。我已将代码与问题一起发布检查我的答案@SidharthJDevCheck我的答案@Sidharth
 NSArray *actualItems= self.viewControllers;

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


    [self setViewControllers:reversedArray animated:YES];