Can I set image as a title to UINavigationBar?

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

提交回复
热议问题