Navigation item title view - same for all pushed view controllers

狂风中的少年 提交于 2019-12-23 07:45:10

问题


How can you keep navigation item title view same for all pushed view controllers?

E.g. I'd like to set title view to a logo that should be visible for all screens.

UIImage *logoImage = [UIImage imageNamed:@"navigation-bar-logo"];
UIImageView *titleLogo = [[UIImageView alloc] initWithImage:logoImage];
self.navigationItem.titleView = titleLogo;

If I set it for each view controller individually, it looks strange with the iOS7 navigation bar animation.


回答1:


One way to do this is to use UINavigationItem.titleView and UINavigationItem.rightBarButtonItem. Like this :

viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
viewController.navigationItem.rightBarButtonItem = item;    

Here I am using UIImageView as custom view, but it can be UIButton with custom image.

Another way which I suggest you is that without using a viewController

// Create your image
UIImage *image = [UIImage imageNamed: @"logo.png"];
UIImageView *imageview = [[UIImageView alloc] initWithImage: image];

// set the text view to the image view
self.navigationItem.titleView = imageview;

Hopefully this info will helps you. Cheers :)




回答2:


If you want to show a Navigation item title view - same for all pushed view controllers

  • Make a ViewController as ParentViewController and all other ViewController as the child of that .Then what ever design You will do in ParentVC ,It will reflect in Child VC.

Below is the example . In appDelegate Class,

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[WARootViewController alloc]init]];
self.window.rootViewController = nav;

In the ViewController ,Set the imageView Frame(optional,You can give your desired) and Image Name(mention the extension if logoImage is giving NULL memory).

    UIImage *logoImage = [UIImage imageNamed:@"navigation-bar-logo.jpg"];
UIImageView *titleLogo = [[UIImageView alloc] initWithImage:logoImage];
titleLogo.frame = CGRectMake(0, 0, 50, 30);
self.navigationItem.titleView = titleLogo;
  • Make Rest of the ViewControllers as child of WARootViewControllers

    #import "WARootViewController.h"
    @interface WAFirstViewController : WARootViewController
    
    #import "WARootViewController.h"
    @interface WASecondViewController : WARootViewController
    


来源:https://stackoverflow.com/questions/22396066/navigation-item-title-view-same-for-all-pushed-view-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!