Can I set image as a title to UINavigationBar?

后端 未结 16 1662
有刺的猬
有刺的猬 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:05

    Just use

    [navController.navigationBar insertSubview:myImage atIndex:0] ;
    

    where myImage is of type UIImageView and navController is of type UINavigationController

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

    You can do it right from storyboard (as of Xcode 7):

    1. Create a view outside main view of view controller. It can be a nested view or just an image
    2. Add navigation item to your view controller
    3. Ctrl+ drag from navigation item and drop on outside view

    4.select title view

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

    I modified the UINavigationBar+CustomImage.m to have the title still visible to the user. Just use insertSubview: atIndex: instead of addSubview:

    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(0, 0, 320, 44);
        [self insertSubview:imageView atIndex:0];
        [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
    
    0 讨论(0)
  • 2020-12-07 10:13

    Add image to naviagtionBar with SWIFT that scales to fit and clips to bounds. You can call this function inside the view controllers viewDidLoad() function.

    func setupNavigationBarWithTitleImage(titleImage: UIImage) {
    
        let imageView = UIImageView(image: titleImage)
        imageView.contentMode = .ScaleAspectFit
        imageView.clipsToBounds = true
        navigationItem.titleView = imageView
    
    }
    
    0 讨论(0)
  • 2020-12-07 10:15

    In MonoTouch you can use this:

     this.NavigationItem.TitleView = myImageView;
    
    0 讨论(0)
  • 2020-12-07 10:16

    The following is how you would do this in (Xamarin's) MonoTouch with C#.NET

    Create a UIViewConvrtoller that is in a NavigationController then call this at any time:

    someNiceViewControllerYouMade.NavigationController.NavigationBar
         .InsertSubview(new UIImageView
         (MediaProvider.GetImage(ImageGeneral.navBar_667x44)),0);
    

    Note: MediaProvider is just a class that fetches images.

    This example allows for the view to fill the full Navigation Bar , and lets the text for the items caption appear too.

    0 讨论(0)
提交回复
热议问题