2013-11-14 29 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

相关问题