2014-04-01 37 views
0

我正在對導航欄進行一些更改,並顯示在appdelegate中。UIAppearance子視圖從導航欄中消失

這是我的方法:

-(void) setAppearance{ 
    NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]]; 
    [titleBarAttributes setValue:[UIFont fontWithName:@"AvantGarde-ExtraLight" size:18] forKey:NSFontAttributeName]; 
    [titleBarAttributes setValue:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; 
    [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes]; 
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:6.0/256.0 green:57.0/256.0 blue:84.0/256.0 alpha:1.0]]; 
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 

    int borderSize = 3; 
    UIImageView *navBorder = [[UIImageView alloc] initWithFrame:CGRectMake(0, 
                      41, 
                      320, 
                      borderSize)]; 

    navBorder.image = [UIImage imageNamed:@"energy_line"]; 

    navBorder.tag = 999; 


    [[UINavigationBar appearance] addSubview:navBorder]; 

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 
} 

我有我的appdelegate的方法是設置窗口給我的第一的viewController,當我把這種方法,我navBorder從導航欄刪除。我不明白爲什麼會發生這種情況,沒有任何代碼可以改變viewcontroller中導航欄中的任何內容。

- (void)rootView 
{ 
    [self.window setRootViewController:initialViewController]; 
} 

回答

0

我回答非常這裏同樣的問題:https://stackoverflow.com/a/26414437/538491

但這裏是一個總結: 調用[[UINavigationBar的外觀]返回接收機類的外觀代理。 addSubview:方法未被標記爲UI_APPEARANCE_SELECTOR。 UIAppearance的代理方法的一個主要缺點是很難知道哪些選擇器兼容。

你應該抓住導航欄並通過調用這個方法在那裏添加圖像:[self.navigationController.navigationBar addSubview:navBorder]或者你應該子類UINavigationBar這給你更多的靈活性。