Can I set image as a title to UINavigationBar?

后端 未结 16 1664
有刺的猬
有刺的猬 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];
    }
    
    0 讨论(0)
  • 2020-12-07 10:30

    Do it quickly using storyboard and @IBDesignable:

    @IBDesignable class AttributedNavigationBar: UINavigationBar {
    
        @IBInspectable var imageTitle: UIImage? = nil {
    
            didSet {
    
                guard let imageTitle = imageTitle else {
    
                    topItem?.titleView = nil
    
                    return
                }
    
                let imageView = UIImageView(image: imageTitle)
                imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
                imageView.contentMode = .scaleAspectFit
    
                topItem?.titleView = imageView
            }
        }
    }
    

    Then in attributes inspector just select an image:

    and wait a second for result:

    So setting view is there where it should be... in storyboard.

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

    If your buttons disappear when you navigate back and forth the navigation, this fixed it for me:

    NSArray *mySubviews = navigationBar.subviews;
    UIImageView *iv = nil;
    
    // 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]])
        {
            NSLog(@"found background at index %d",i);
            iv = [mySubviews objectAtIndex:i];
            [[mySubviews objectAtIndex:i] removeFromSuperview];
            [navigationBar insertSubview:iv atIndex:0];
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:30

    ios5.0 introduced a heap of features to customise the appearance of standard elements. If you didn't want to use an ImageView for the title, an alternative would be to customise the appearance of all UINavbars using a background image and a custom font/colour.

    - (void) customiseMyNav
    {
        // Create resizable images
        UIImage *portraitImage = [[UIImage imageNamed:@"nav_bar_bg_portrait"] 
            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        UIImage *landscapeImage = [[UIImage imageNamed:@"nav_bar_bg_landscape"] 
            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
        // Set the background image
        [[UINavigationBar appearance]  setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance]  setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone];
    
        // set the title appearance
        [[UINavigationBar appearance] setTitleTextAttributes:
            [NSDictionary dictionaryWithObjectsAndKeys:
                [UIColor colorWithRed:50.0/255.0 green:150.0/255.0 blue:100/255.0 alpha:1.0], 
                UITextAttributeTextColor, 
                [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6], 
                UITextAttributeTextShadowColor, 
                [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
                UITextAttributeTextShadowOffset, 
                [UIFont fontWithName:@"Arial-Bold" size:0.0], 
                UITextAttributeFont, 
                nil]];
    }
    
    0 讨论(0)
提交回复
热议问题