2013-11-14 28 views
15

我想改變我的iOS7應用程序UINavigationBar的外觀。我做了以下內容:iOS 7 UINavigationBar外觀不工作的第一次...

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    m_sNumberToCall = @""; 

    UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(btHomeTouched:)]; 
    self.navigationItem.leftBarButtonItem = btn; 

    self.navigationController.navigationBar.translucent = YES; 


    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault]; 

    NSShadow * shadow = [[NSShadow alloc] init]; 
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; 
    shadow.shadowOffset = CGSizeMake(0, 1); 
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
                  [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], 
                  NSForegroundColorAttributeName, 
                  shadow, 
                  NSShadowAttributeName, 
                  [UIFont fontWithName:@"Helvetica-Bold" size:21.0], 
                  NSFontAttributeName, 
                  nil]]; 
} 

但是,我第一次呈現的UITableViewController它是標準的iOS7導航欄,然後按我家,再次呈現它,它是我的新面貌。

任何想法,爲什麼它不能在第一次工作?

+0

嘗試在viewDidAppear中移動代碼 – Ilario

+0

我在那裏嘗試過,沒有去過,我也試過在viewWillAppear。 – LilMoke

+1

對於任何其他可能遇到山姆問題的人,我將此代碼:[[UINavigationBar appearance] setBa ...更改爲:[self.navigationController.navigationBar setBa ...以及setTitleTextAttributes行中的代碼。找到答案在這裏:http://stackoverflow.com/questions/17361500/how-to-set-navigation-bar-image-ins-ios-7 – LilMoke

回答

26

請勿直接更改外觀,而是更改導航欄。外觀隻影響未來的實例,但不影響已經創建的實例。

變化:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault]; 

到:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault]; 
+6

我實際上有同樣的問題,我設置外觀之前我的導航控制器被實例化... –

+0

這解釋了很多! – kokluch

+0

真正的解決方案如下面的@ fabf98dev所回答。您不需要使用navigationController,只需確保在顯示第一個viewController之前調用此行(UINavigationBar.Appear ..)。 –

2

答案之前,不僅可以幫助你與背景圖像而不是與title text attributes

你不需要改變你的代碼,但所有你需要做的就是在你的AppDelegate.m文件移動到

applicationDidFinishLaunchingWithOptions

相關問題