How to resize an UITabBar selection indicator image in landscape

822 views Asked by At

I'm customizing a UITabBar in a UITabBarController subclass ("CustomTabBarVC"). I wanted to use a custom selection indicator image, so I wrote this in the "CustomTabBarVC" implementation:

- (void)viewWillLayoutSubviews
{
   CGFloat tabItemWidth;
   UIView *tabItem = [[[self.tabBar items] lastObject] view];
   tabItemWidth = tabItem.frame.size.width;

   UIImage *selectionIndicator;

   // Set here 'selectionIndicator' to an UIImage which fits 'tabItemWidth' and tab bar's height

   [[UITabBar appearance] setSelectionIndicatorImage:selectionIndicator];
}

This works fine when the view is loaded in portrait, but when I change the orientation of the device to landscape, though the method is called again and selectionIndicator image seems to take the new width, it is no redrawn and the previous image keeps showing. How could I handle this?

Thanks

1

There are 1 answers

0
shankar On

When your device orientation get changed, the default delegate will get called and you get your orientation in this method.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 // do something before rotation 
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
 // do something after rotation
}

Then implement viewWillAppear: and this method will get called after the orientation delegate. Now you can change your TabBar image in this method.