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

后端 未结 7 2154
耶瑟儿~
耶瑟儿~ 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 05:58

    Perhaps not what you meant, but I ran into this page looking for a way to provide a centered background image for the navigation bar, so in case you're here for that... here's one way.

    Stealing a little bit from another answer, you can break your image into foreground and background, then build a new image that stretches the background and centers the foreground, and then set that as your nav bar's background image. Building the image works like so:

    // build an image by stretching the bg, then merging it with the fg
    CGSize barSize = self.navController.navigationBar.frame.size;
    UIImage *fg = [UIImage imageNamed:@"some_fg"];
    UIImage *bg = [[UIImage imageNamed:@"some_bg"]
                   resizableImageWithCapInsets:UIEdgeInsetsMake(0.f,1.f,0.f,1.f)];
    UIGraphicsBeginImageContextWithOptions(barSize, NO, 0.0);
    [bg drawInRect:CGRectMake(0, 0, barSize.width, barSize.height)];
    [fg drawInRect:CGRectMake((barSize.width-fg.size.width)/2.f,
                              0,
                              fg.size.width,
                              fg.size.height)];
    // grab the merged images
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    0 讨论(0)
  • 2021-02-01 05:59

    You just specify it's frame by

    logoView.frame = CGRectMake(initialize frame here);
    

    Then use the following

    [self.navigationItem setTitleView:logoView];
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • First we have to create a view which have size as same as navigation bar then add an image view and set set its frame as it looks center in the navigation bar.It works for all ios version and it automatically takes frame size as per device (retina or normal) and works like magic.

    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, 320, 44);
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Header.png"]];
    imgView.frame = CGRectMake(75, 0, 150, 44);
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    
    [headerView addSubview:imgView];
    
    navigationCtrl.navigationBar.topItem.titleView = headerView;
    
    [headerView release];
    [imgView release];
    
    0 讨论(0)
  • 2021-02-01 06:07
    func centeredNavBarImage (){
        let navcontroller = navigationController!
        let image = #imageLiteral(resourceName: "yourImage")
        let imageView = UIImageView(image:image)
    
        let bannerWidth = navcontroller.navigationItem.accessibilityFrame.size.width
        let bannerHeight = navcontroller.navigationBar.frame.size.height
    
    
        let bannerX = bannerWidth / 2 - image.size.width / 2
        let bannerY = bannerHeight / 2 - image.size.height / 2
    
        imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
        imageView.contentMode = .scaleAspectFit
    
        navigationItem.titleView = imageView
    
    }
    

    This is a modified code from https://youtu.be/bLkuu_fmlsU

    The bannerWidth takes into account a refresh button item I have on the right side of the navbar. Seems to work.

    Run the function on ViewDidLoad

    0 讨论(0)
  • 2021-02-01 06:10

    Swift:

    var logoImage:UIImage = UIImage(named: "logo_text")!
    self.navigationItem.titleView = UIImageView(image: logoImage)
    
    0 讨论(0)
提交回复
热议问题