2012-08-01 59 views
1

所以我不得不承認,我有這個代碼工作完美,直到一天前,當我添加一個新的視圖,所以我現在很沮喪。故事板上的tabbar上的自定義背景

設置: 我有一個包含tabbar的storyboarded應用程序。在AppDelegate中,我有在AppDelegate中安裝的CoreData

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
    UINavigationController *navigationController = [[tabBarController viewControllers] objectAtIndex:0]; 
    GamesViewController *controller = [[navigationController viewControllers] objectAtIndex:0]; 
    controller.managedObjectContext = self.managedObjectContext; 

    return YES; 
} 

而且下面,我有這種方法,將標準的選項卡背景設置爲我選擇的圖片:

- (void)customizeInterface {  
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"]; 
    [[UITabBar appearance] setBackgroundImage:tabBarBackground]; 
} 

所以這一切工作正常,直到我添加了另一個登錄視圖之前我的選項卡。我不得不改變CoreData最初的設置(從我的標籤頁到我的登錄/初始化視圖)。以下是故事板外觀的新設置。

Here's the new setup in my storyboard

現在,當應用程序加載了...背景圖像最初出現過,但在第一個選項卡上。一旦我點擊關閉,它會再次切換到默認的漸變色。如果我回到第一個/初始選項卡,背景不會自行重新應用,它將保留爲彩色漸變。

下面是修訂後的applicationDidFinishLaunching代碼與它一起去:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    //instantiate local context 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    if (!context) { 
     // Handle the error. 
     NSLog(@"Error: Context is null"); 
    } 

    LoginViewController *rootViewController = [LoginViewController alloc]; 
    rootViewController.managedObjectContext = context; 

    return YES; 
} 

,那麼什麼我試圖做的,是進入viewDidLoad中的第一個VC,我的TabBar負載高達(GameViewController ),並試圖將這一來解決這個問題:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"dock_background"]]; 
} 

這並沒有工作,所以我使用相同的原始代碼,我在我的AppDelegate也嘗試並且還沒有工作:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIImage* tabBarBackground = [UIImage imageNamed:@"tab_background"]; 
    [[UITabBar appearance] setBackgroundImage:tabBarBackground]; 
} 

所以......我有點卡住。我必須做(或不做)如此明顯的事情......任何人都有任何提示/指針?

由於一噸 - 德魯

+0

你想定製的TabBar – 2012-08-01 04:29:00

+0

哇。現在我看到編碼的深夜效應有時可以做到。原來,答案始終在我面前。我的意見是試圖加載圖像「dock_background.png」而不是tab_background.png(這是實際的文件名)。由於該圖像不存在...它默認爲正常的漸變背景顏色。衛生署!感謝Neon的幫助......我將在未來的項目中記住您的代碼,因爲它看起來很有前途。 – Drew 2012-08-01 13:54:38

回答

0

Delegate.h文件

@interface AppDelegate中:UIResponder

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) UIImageView *imgV; 
@property (strong, nonatomic) UIViewController *viewController; 
@property (strong, nonatomic) UITabBarController *tabBarController; 

@end 

Delegate.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

     self.viewController = [[DeskboardVctr alloc] initWithNibName:@"DeskboardVctr" bundle:nil]; 




    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.delegate=self; 
    self.imgV=[[UIImageView alloc] init]; 
    self.imgV.frame=CGRectMake(0, 0, 1024, 49); 
    [[self.tabBarController tabBar] insertSubview:self.imgV atIndex:1]; 
    self.tabBarController.delegate=self; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

的TabBar的委託方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ 
    NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController]; 
     switch (index) { 
      case 0: 
       self.imgV.image=[UIImage imageNamed:@"t1.png"]; 
       break; 
      case 1: 
       self.imgV.image=[UIImage imageNamed:@"t2.png"]; 
       break; 
      case 2: 
       self.imgV.image=[UIImage imageNamed:@"t3.png"]; 
       break; 
      case 3: 
       self.imgV.image=[UIImage imageNamed:@"t4.png"]; 
       break; 
      case 4: 
       self.imgV.image=[UIImage imageNamed:@"t5.png"]; 
       break; 
      default: 
       break; 
     } 
    return YES; 
} 

希望你可以幫助這個 感謝

+0

欲瞭解更多詳情和示例,請訪問參考鏈接http://bit.ly/NLv64P – 2012-08-01 04:55:53