15

我正在尝试更改 iOS7 应用程序中 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 导航栏,然后我按下 home 并再次展示它,这是我的新外观。

任何想法为什么它第一次不起作用?

4

2 回答 2

28

不要改变外观而是直接改变导航栏。外观只影响未来的实例,而不影响已经创建的实例。

改变:

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

至:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
于 2013-11-14T19:00:52.640 回答
2

之前的答案只对背景图像有帮助,对title text attributes.

您不需要更改您的代码,但您所要做的就是将其移至

applicationDidFinishLaunchingWithOptions

在你的AppDelegate.m文件中。

于 2014-10-27T09:36:28.410 回答