简体   繁体   English

如何在AppDelegate文件的XCode5 iOS7中自定义导航栏?

[英]How to Customize navigation bar in XCode5 iOS7 in AppDelegate file?

I am trying to customise the navigation bar of my iPhone app. 我正在尝试自定义iPhone应用程序的导航栏。 I want to add an image in the left side of the navigation bar title. 我想在导航栏标题的左侧添加一个图像。 I tried the following code from the didFinishLaunchingWithOptions of the AppDelegate.m file: 我从AppDelegate.m文件的didFinishLaunchingWithOptions中尝试了以下代码:

UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo.png"]];
    [myView addSubview:image];
    [self.navigationController.navigationBar addSubview:myView];
    self.navigationController.navigationItem.titleView=myView;

but it doesn't work. 但这不起作用。 When I try it from the viewDidLoad of one of my ViewControllers it works. 当我从我的一个ViewController的viewDidLoad中尝试它时,它就可以工作。 But i want to inherit this behaviour in all my ViewControllers 但是我想在我所有的ViewController中继承这种行为

UINavigationItem has a property, titleView. UINavigationItem具有一个属性titleView。 You need to set that to your custom view, and then that view will replace the standard title (so you need to have your image and a label for the title in your custom view). 您需要将其设置为自定义视图,然后该视图将替换标准标题(因此您需要在自定义视图中具有图像和标题标签)。

self.navigationItem.titleView = myView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM