Can I set image as a title to UINavigationBar?

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

    I modified the UINavigationBar+CustomImage code to properly work without leaking memory.

    - (void)setBackgroundImage:(UIImage *)image
    {
        if (! image) return;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        [self addSubview:imageView];
        [imageView release];
    }
    
    - (void) clearBackgroundImage
    {
        // This runs on a separate thread, so give it it's own pool
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSArray *mySubviews = self.subviews;
    
        // Move in reverse direction as not to upset the order of elements in the array
        for (int i = [mySubviews count] - 1; i >= 0; i--)
        {
            if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
            {
                [[mySubviews objectAtIndex:i] removeFromSuperview];
            }
        }
    
        [pool release];
    }
    

提交回复
热议问题