Can I set image as a title to UINavigationBar?

后端 未结 16 1917
有刺的猬
有刺的猬 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条回答
  •  旧时难觅i
    2020-12-07 10:28

    I have created a custom category for UINavigationBar as follows

    UINavigationBar+CustomImage.h

    #import 
    
    @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];
    

提交回复
热议问题