2

我想在UINavigationBar. 这是viewDidLoad我要自定义其导航栏的视图控制器的方法中的代码:

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

[[UINavigationBar appearance] setTitleTextAttributes: @{
                            UITextAttributeTextColor: [UIColor greenColor],
                      UITextAttributeTextShadowColor: [UIColor blackColor],
                     UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                 UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
 }];

但是,仅设置了背景图像,并且标题保持为默认的白色和系统字体。我究竟做错了什么?

提前致谢

4

1 回答 1

0

自定义应用程序中的每一个UINavigationBar 将外观代码移动到 AppDelegate 的application:didFinishLaunchingWithOptions:方法中,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [[UINavigationBar appearance] setTitleTextAttributes: @{
                            UITextAttributeTextColor: [UIColor greenColor],
                      UITextAttributeTextShadowColor: [UIColor blackColor],
                     UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                 UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
    }];

    return YES;
}

在您的应用程序中仅自定义一个特定UINavigationBar的: 将以下代码放入您的 ViewController 的viewDidLoad:方法中。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                       UITextAttributeTextColor: [UIColor greenColor],
                                 UITextAttributeTextShadowColor: [UIColor blackColor],
                                UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                            UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
    }];

}
于 2013-08-12T20:01:07.133 回答