Can I set image as a title to UINavigationBar?

后端 未结 16 1663
有刺的猬
有刺的猬 2020-12-07 09:38

Is it possible to set some image as title of Navigation bar?

I think NYTimes application used a Navigation bar and title is look like image file (the reason why it\'

相关标签:
16条回答
  • 2020-12-07 10:22

    This line will work for you, I always use this

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageNavBar.png"]  forBarMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2020-12-07 10:23

    This also works well too

    [self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]];
    
    0 讨论(0)
  • 2020-12-07 10:23

    For those who have the same error but in Xamarin Forms, the solution is to create a Renderer in iOS app and set the image like so :

    [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.Page), typeof(MyApp.Renderers.NavigationPageRenderer))]
    namespace MyApp.Renderers
    {
        #region using
    
        using UIKit;
        using Xamarin.Forms.Platform.iOS;
    
        #endregion
    
        public class NavigationPageRenderer : PageRenderer
        {
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                SetTitleImage();
            }
    
            private void SetTitleImage()
            {
                UIImage logoImage = UIImage.FromFile(ResourceFiles.ImageResources.LogoImageName);
                UIImageView logoImageView = new UIImageView(logoImage);
    
                if (this.NavigationController != null)
                {
                    this.NavigationController.NavigationBar.TopItem.TitleView = logoImageView;
                }
            }
        }
    }
    

    Hope it helps someone!

    0 讨论(0)
  • 2020-12-07 10:25

    You can use an UIImageView for the UINavigationItem.titleView property, something like:

    self.navigationItem.titleView = myImageView;
    
    0 讨论(0)
  • 2020-12-07 10:27

    I find that a transparent .png at about 35px in height has worked well.

    - (void)awakeFromNib {
    
    //put logo image in the navigationBar
    
    UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
    self.navigationItem.titleView = img;
    [img release];
    
    }
    
    0 讨论(0)
  • 2020-12-07 10:28

    I have created a custom category for UINavigationBar as follows

    UINavigationBar+CustomImage.h

    #import <UIKit/UIKit.h>
    
    @interface UINavigationBar (CustomImage)
        - (void) setBackgroundImage:(UIImage*)image;
        - (void) clearBackgroundImage;
        - (void) removeIfImage:(id)sender;
    @end
    

    UINavigationBar+CustomImage.m

    #import "UINavigationBar+CustomImage.h"
    
    @implementation UINavigationBar (CustomImage)
    
    - (void) setBackgroundImage:(UIImage*)image {
        if (image == NULL) return;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(110,5,100,30);
        [self addSubview:imageView];
        [imageView release];
    }
    
    - (void) clearBackgroundImage {
        NSArray *subviews = [self subviews];
        for (int i=0; i<[subviews count]; i++) {
            if ([[subviews objectAtIndex:i]  isMemberOfClass:[UIImageView class]]) {
            [[subviews objectAtIndex:i] removeFromSuperview];
        }
       }    
    }
    
    @end    
    

    I invoke it from my UINavigationController

    [[navController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:image];
    
    0 讨论(0)
提交回复
热议问题