Navigation item title view - same for all pushed v

2019-06-16 20:57发布

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.

2条回答
祖国的老花朵
2楼-- · 2019-06-16 21:35

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
    
查看更多
该账号已被封号
3楼-- · 2019-06-16 21:58

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 :)

查看更多
登录 后发表回答