Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set image as a title to UINavigationBar?

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's seems UINavigationBar is because they use right button to search).

like image 393
Amit Vaghela Avatar asked Nov 14 '08 07:11

Amit Vaghela


4 Answers

You can use an UIImageView for the UINavigationItem.titleView property, something like:

self.navigationItem.titleView = myImageView;
like image 110
Kendall Helmstetter Gelner Avatar answered Nov 16 '22 16:11

Kendall Helmstetter Gelner


I find that a transparent .png at about 35px in height has worked well.

- (void)awakeFromNib {

//put logo image in the navigationBar

UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
self.navigationItem.titleView = img;
[img release];

}
like image 26
Clifton Burt Avatar answered Nov 16 '22 18:11

Clifton Burt


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

enter image description here

4.select title view

enter image description here

like image 13
Cinoss Avatar answered Nov 16 '22 18:11

Cinoss


I have created a custom category for UINavigationBar as follows

UINavigationBar+CustomImage.h

#import <UIKit/UIKit.h>

@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];
like image 8
adam Avatar answered Nov 16 '22 17:11

adam