Search code examples
iosobjective-cxibuitabbaruitabbaritem

TabBar Issue in LandScapeMode


I Created one Demo App For TabBar, in that i took one .xib and in that i added one UITabBar, and i added two UITabBarItem. And When i am run the application in portrait mode it is working fine and design looks good. in portrait mode design look like below

enter image description here

But When i am rotating device to landscape that time image and title alignment changed, it is not showing proper, landscape mode image is look like below

enter image description here

Can you please suggest me solution for this. Note - I am not using TabBarController, only TabBar and TabBarItems


Solution

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

    1. If you application not supporting dark mode(i mean you disable dark mode) then you need to add this method

      -(UITraitCollection *)traitCollection {
              if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                     return [super traitCollection];
              }else{
                     return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
              }
      }
      
    2. If your application supporting dark mode then you need to add this method

      -(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.