簡體   English   中英

UITabBar外觀setSelectionIndicatorImage在首次啟動iOS7時不起作用

[英]UITabBar appearance setSelectionIndicatorImage does not work on first launch iOS7

我有一個自定義的UITabBar並在AppDelegate中使用以下代碼:

- (void)tabBarController:(MainUITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self customizeTabBar];
}


- (void)customizeTabBar {

    NSLog(@"*******customizeTabBar*******");
    UIImage *tabBackground = [[UIImage imageNamed:@"unselectedtab"]
                  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    // Set background for all UITabBars
    [[UITabBar appearance] setBackgroundImage:tabBackground];
    // Set tint color for the images for all tabbars
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
    // Set selectionIndicatorImage for all tabbars
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"selectedtab"]];

} 

- (void)tabBarController:(MainUITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    NSLog(@"*******didEndCustomizingViewControllers*******");
}

這在iOS5 +中都很好,但在第一次加載第一個TabBarItem時,項目指示器為白色並且按鈕似乎已被選中,但未加載“selectedTab”圖像。

當我按下另一個選項卡時,新選項卡顯示為紅色且顯示正確 - 與此后選擇的第一個或任何選項卡欄項一樣 - 它僅在首次啟動時不起作用。

調用customizeTabBar但首次啟動時不會顯示所選圖像。

didEndCustomizingViewControllers似乎根本沒有被調用。

這在iOS7上的模擬器或設備中不起作用 - 但在iOS5,6上也是如此。

有任何想法嗎? 提前致謝。

再次設置標簽欄的選擇指示圖像,除了通過外觀做,對我有用!

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

我看到了同樣的問題。 這是我的didFinishLaunching

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self applyStyleSheet];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.backgroundColor = [UIColor redColor];
    self.window.tintColor = [UIColor whiteColor];
    UITabBarController *tabBarController = [self setupTabBarController];
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

以下是我設置標簽欄的方法:

- (UITabBarController *)setupTabBarController
{
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:[[SecondViewController alloc] init]];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:[[ThirdViewController alloc] init]];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:[[FourthViewController alloc] init]];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:[[FifthViewController alloc] init]];
    [tabBarController setViewControllers:@[nav1, nav2, nav3, nav4, nav5]];

    return tabBarController;
}

最后,這是標簽欄自定義塊:

- (void)applyStyleSheet
{
    UITabBar *tabBar = [UITabBar appearance];
    [tabBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]]];
    [tabBar setTintColor:[UIColor whiteColor]];
    [tabBar setSelectionIndicatorImage:[UIImage imageNamed:@"tab-selected"]];
    [tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}

如上所述,“選項卡選擇”圖像未加載到第一個選項卡上。 但是,我在[self.window makeKeyAndVisible]之后添加了以下行,以便我的選項卡啟動時打開了另一個選項卡,並且“tab-selected”圖像確實顯示在此選項卡上:

    [tabBarController setSelectedIndex:1];

所以這是我最終確定的didFinishLaunching與微妙的黑客,使它工作:)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self applyStyleSheet];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window.backgroundColor = [UIColor redColor];
        self.window.tintColor = [UIColor whiteColor];
        UITabBarController *tabBarController = [self setupTabBarController];
        self.window.rootViewController = tabBarController;
        [self.window makeKeyAndVisible];
        [tabBarController setSelectedIndex:1];
        [tabBarController setSelectedIndex:0];

        return YES;
    }

好。

不是最好的修復,但嘿必須提交。

刪除appdelegate中的自定義代碼以及TabBars屬性檢查器上的項目xib文件(是一個舊項目)(使用xcode 5) - 添加選項卡欄背景和選擇圖像。

這適用於ios7,無需appdelegate中的任何自定義代碼。

對於iOS5 + 6之前的版本(這個應用程序僅支持5+)但是我們仍然需要代碼,所以我添加了一個簡單的版本檢查並保持代碼不變:

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

if(SYSTEM_VERSION_LESS_THAN(@"7.0"))

    {

        UIImage *tabBackground = [[UIImage imageNamed:@"unselectedtab"]

                                  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

        // Set background for all UITabBars

        [[UITabBar appearance] setBackgroundImage:tabBackground];
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];

    // Set tint colour for the images for all tabbars

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

    // Set selectionIndicatorImage for all tabbars

    [[UITabBar appearance] setSelectionIndicatorImage:nil];

    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"selectedtab.png"]];

}

我想我在iOS 7中為新應用程序設計時遇到了同樣的問題!! iOS 7已經構建了更多不同的東西,因為我們都習慣了不同的東西。

據我所知,我們都在使用StoryBoards,並且無法在我們的代碼中集成Segues! :)所以我選擇不搞亂代碼 ,在我嘗試了大部分關於此的StackOverFlow答案之后! :)因為,為什么你想這樣做,當你給了一個Goody Good Interface Builder(IB)Story Boarding Tool

題:
當我們設置了我們的選項標簽圖像,專門用於標簽欄的背景圖像時,它沒有顯示我們在代碼中設置的圖像選擇了哪個標簽... ???


以下是我為解決這個問題而做的StoryBoard設置的截圖!

從您的via document outline面板中選擇您的TabBarController:
從via document document面板中選擇TabBarController

從“工具”面板設置選項卡欄的設置:
從“實用工具”面板設置選項卡欄的設置

然后你的程序設置運行! 它現在知道當應用程序首次顯示第一個選項卡視圖時選擇第一個選項卡,並且當選擇每個選項卡時,應該為所有選項卡欄指示器顯示哪個圖像! :)
希望你們都有線索! 如果我幫助你我很開心! 但如果我浪費你的時間,我很抱歉! :(但相信我,這讓我像個魅力!

- (void)customizeTabBar {

 UIImageView *customizeTabBar = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
customizeTabBar.image=[UIImage imageNamed:@"Tab_bar.png"];

firstTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab1.png"] highlightedImage:[UIImage imageNamed:@"tab11.png"]];
[firstTab setFrame:CGRectMake(8.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: firstTab];

secondTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab2"] highlightedImage:[UIImage imageNamed:@"tab22"]];
[secondTab setFrame:CGRectMake(115.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: secondTab];

thirdTab = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tab3"] highlightedImage:[UIImage imageNamed:@"tab33"]];
[thirdTab setFrame:CGRectMake(223.0,01.0,90.0,49.0)];
[customizeTabBar addSubview: thirdTab];
self.tabBar.tag=10;
[self.tabBar addSubview:customizeTabBar];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM