如何在横向中调整UITabBar选择指示器图像的大小

时间:2015-06-16 09:33:44

标签: ios uiimage uitabbarcontroller uitabbar uitabbaritem

我正在UITabBar子类(“CustomTabBarVC”)中自定义UITabBarController。我想使用自定义选择指标图像,所以我在“CustomTabBarVC”实现中写了这个:

- (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];
}

当以纵向方式加载视图时这可以正常工作,但是当我将设备的方向更改为横向时,虽然再次调用该方法并且selectionIndicator图像似乎采用了新的宽度,但是没有重新绘制并且之前的图像不断显示。我怎么能处理这个?

由于

1 个答案:

答案 0 :(得分:0)

当您的设备方向发生变化时,默认代理将被调用,您将获得此方法的定位。

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

然后实现viewWillAppear:并且在方向委托之后将调用此方法。现在,您可以在此方法中更改TabBar图像。