【问题标题】:Selection Indicator of tab bar not working标签栏的选择指示器不起作用
【发布时间】:2016-07-28 09:00:15
【问题描述】:

您好,我是 iOS 新手。

我已经通过 Storyboard 实现了一个 tabBarController 以及 4 个 tabBar 项。现在,我需要自定义标签栏,如下图所示。我已经为标签栏设置了背景。

 + (UIImage *)imageFromColor:(UIColor *)color {
     CGRect rect = CGRectMake(0, 0, 1, 1);
     UIGraphicsBeginImageContext(rect.size);
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGContextSetFillColorWithColor(context, [color CGColor]);
     CGContextFillRect(context, rect);
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return image;
 }


 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

      [[UITabBar appearance] setTintColor:[UIColor whiteColor]];

      [[UITabBar appearance] setBackgroundImage:[AppDelegate imageFromColor:[UIColor blackColor]]];

      return YES;
   }

标签栏的背景设置没有任何问题。

当我尝试为选定的标签栏项目设置颜色时,它不起作用。不知道为什么?

[[UITabBar appearance]setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor orangeColor]]];

我需要像这样自定义我的标签栏:

我该怎么做?

【问题讨论】:

    标签: ios objective-c uitabbarcontroller


    【解决方案1】:

    首先你应该记住UITabBar的配置应该在AppDelegate.m:

    您应该添加[[UITabBar appearance]setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor orangeColor]]]; AppDelegate.mapplicationdidFinishLaunchingWithOptions 方法中。

    其次,在您的情况下,您应该提供标签栏项目选中状态示例:

       UITabBarController *tabBarController = (UITabBarController   *)self.window.rootViewController;
       UITabBar *tabBar = tabBarController.tabBar;
       UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    
       //We set title
       tabBarItem1.title = @"Home";
    
       //We set highlighted state
       [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];
    
       // Change the tab bar background
       UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
       [[UITabBar appearance] setBackgroundImage:tabBarBackground];
       // Tab bar active background
       [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
    

    【讨论】:

    • 我已将其添加到 AppDelegate.m 本身
    • 即使这只是设置背景
    【解决方案2】:

    将选择指示器图像直接设置到标签栏以及外观对我有用。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
         UITabBarController *tabBarContr = (UITabBarController *)self.window.rootViewController;
        [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_bar_selection_indicator.png"]];
    
        // iOS7 hack: to make selectionIndicatorImage appear on the selected tab on the first app run
        [[tabBarContr tabBar] setSelectionIndicatorImage:[UIImage imageNamed:@"tab_bar_selection_indicator.png"]];
    
        return YES;
    }
    

    【讨论】:

    • 即使这样我得到相同的结果
    • 只是尝试附上屏幕截图问题你得到什么可能有助于更清楚地解决问题
    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多