【问题标题】:Customize UITabBarItem自定义 UITabBarItem
【发布时间】:2012-07-31 10:02:06
【问题描述】:

我知道在 iOS 5 中可以使用自定义 UITabBarItem

[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel"]];

tabbar_sel 图像的宽度为 120px (640px/5)。对于横向模式,我需要将其更改为 190x 宽度的图像。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    NSLog(@"landscape.");
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel_l"]];
    } else {
    NSLog(@"normal.");
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel"]];
    }
}

但是,这不起作用,无论是在委托类中还是在 ViewController 中。 我也已经尝试过了,但它会导致崩溃。

[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel"] 
forBarMetrics:UIBarMetricsDefault];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_sel_l"] forBarMetrics:UIBarMetricsLandscapePhone];

【问题讨论】:

    标签: iphone cocoa ios5 proxy appearance


    【解决方案1】:

    willRotateToInterfaceOrientation: 将在视图加载时调用以检查它应该自动旋转的方向,但是它不会在每次尝试旋转时都询问这个。您可以观察方向何时发生变化:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    - (void) orientationChange: (NSNotification *) notification {
      UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
      if (orientation == UIDeviceOrientationPortrait) {
         NSLog(@"portrait");
      }else if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
         NSLog(@"landscape");
      }
    }
    

    【讨论】:

    • 我检查了它并编辑了我的代码,这应该可以表明从纵向到横向的变化,反之亦然。但是,我并没有声称结合您提供的代码会产生所需的效果。我不确定通过使用 setSelectionIndicatorImage: 它也会自动重新绘制。
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 2021-02-13
    • 2011-02-18
    • 1970-01-01
    相关资源
    最近更新 更多