uinavigationbar背景图像隐藏标题

时间:2013-11-06 06:35:17

标签: ios iphone ipad uinavigationbar title

当我将背景图片添加到uinavigationbar时,它会隐藏uinavigationbar上的标题,

这是我的代码,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       ...
       ...

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

类中的代码:

    int height = self.navigationController.navigationBar.frame.size.height;
    int width = self.navigationController.navigationBar.frame.size.width;

    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    navLabel.text=@"Categories";
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textColor = [UIColor whiteColor];
    navLabel.font = [UIFont boldSystemFontOfSize:17];
    navLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = navLabel;

编写此代码后,我无法在导航栏上看到标题,标题位于navigationBar图像后面。

请提供帮助,提前致谢..

2 个答案:

答案 0 :(得分:2)

而不是将标签设置为UINavigationBar的titleView,将UILabel添加为NavigationBar的子视图,如下所示......

[self.navigationController.navigationBar addSubview:navLabel];
[self.navigationController.navigationBar bringSubviewToFront:navLabel];

答案 1 :(得分:0)

如果您只想自定义navigationBar标题,我建议您使用外观而不是在导航栏中添加标签。

     [[UINavigationBar appearance] setTitleTextAttributes:@{
                                 UITextAttributeFont: [UIFont fontWithName:yourFontName size:fontSize],
                                 UITextAttributeTextColor :[UIColor whiteColor],

      }];