Search code examples
iosobjective-ctabbaruitabbaritem

UITabBarItem Title Text Not Align Centrally, When We Set Device in Landscape Mode in iOS13


I am implementing TabBar Dynamically in to my Objective-C iOS Application. In that, All TabBarItem Title Showing properly in Portrait mode, But when I am rotating device in Landscape mode that time TabBarItem Title position not showing centrally.

Here I am attaching my portrait screenshot of TabBar.

enter image description here

And When I rotate device and showing title alignment issue is look like this

enter image description here

For Fixing this issue i got one solution is here :

- (UITraitCollection *)traitCollection {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return [super traitCollection];
    }else{
        return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
    }
}

If I am using this solution then it is working fine in LandScape Mode, but when i add this method in my code, it stopped dark mode support in my ViewController.

So I want proper alignment into LandScape mode and also I want to support dark mode in my ViewController also.

Note : This issue happens only in iOS13 Device and only in iPhone (below iOS13 in all iPhone device, it is working fine and in iPad, it is working fine in any iOS version.)

AnyOne Can you please help me. Any Help is Appreciate. Thanks


Solution

  • I found the solution of this problem, you want to add one method into your viewController.m file which have a TabBar.

    If your application supporting dark mode then you need to add this method, it will work for all iOS device

        -(UITraitCollection *)traitCollection {
             if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                  return [super traitCollection];
             }else{
                  UITraitCollection *superSizeClass = [super traitCollection];
                  UITraitCollection *verticalSizeClass = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
    
                  NSArray *combinedTraitArray = [NSArray arrayWithObjects: superSizeClass,verticalSizeClass, nil];
    
                  UITraitCollection *combinedTraits = [UITraitCollection traitCollectionWithTraitsFromCollections: combinedTraitArray];
                  return combinedTraits;
            }
        }
    

    It will work for this issue. Thanks.