How to put an image in the center of navigationBar of a UIViewController?

后端 未结 7 2153
耶瑟儿~
耶瑟儿~ 2021-02-01 05:31

I have this snippet of code used in viewDidLoad of a UIViewController. I\'va no errors. Images exists. I get the background but not the image. Image is a sort of logo.



        
7条回答
  •  隐瞒了意图╮
    2021-02-01 06:03

    The UINavigationController manages the navigation bar by looking at the navigationItem property of the top-most view controller on the navigation stack. So to change the view to a logo, you need to set this up in the view controller that uses the logo (i.e. the root view controller or another one that gets pushed on the stack).

    Do something like this in viewDidLoad of your view controller:

    UIImage* logoImage = [UIImage imageNamed:@"logo.png"];
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logoImage];
    

    In your case, you are setting the wrong navigation item:

    // Oops...
    self.navigationController.navigationItem.titleView = logoView;
    // Should be this:
    self.navigationItem.titleView = logoView;
    

提交回复
热议问题