GAMA
GAMA

Reputation: 5996

image in title of navigation bar in objective c

I have made an iPad application, in that I used navigation control,

now in the title bar , I want to put image on left side, so I hide title bar with label, but label is not covering entire width of title bar,

IL APP IN THE SCREEN SHOT,

here is the code snippet,

UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        titleView.textColor = [UIColor yellowColor]; // Change to desired color


        titleView.backgroundColor=[UIColor blackColor];

        titleView.text = @"IL APP";
        titleView.textAlignment=UITextAlignmentCenter;
        //self.navigationItem.titleView = titleView;
        [self.navigationItem setTitleView:titleView];
        [titleView release];
    }

screen

Upvotes: 0

Views: 5141

Answers (3)

evilGenius77
evilGenius77

Reputation: 1

Hi i wanted to do the same thing and i saw your posting, i basically used some of the code you provided and modified it a little and got it to work see below:

//declare and initialize your imageView
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView;

titleImage = [[UIImageView alloc] 
initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) 
- (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)];

//setting the image for UIImageView
titleImage.image = [UIImage imageName:@"photo.jpg"];

//NOTE: you have to do this line at the bottom to add image to the navigation bar Try it it WORKS!

self.navigationItem.titleView = titleImage;

Upvotes: 0

Hiren
Hiren

Reputation: 12780

I tried to solved your problem but I get the same result.

But you can hide navigation bar and add the UILabel in to your ViewController

self.navigationController.navigationBarHidden = FALSE;
[self.view addSubview:titleview];

UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"navImage.png"]];
[self.navigationController.navigationBar addSubview:imagenav];

[self.navigationController.navigationBar sendSubviewToBack:imageNav];
[imageNav release];

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 3013

@Gama as far as your question is concerned you are asking to put an Image but your real issue being described is that the label is not covering up the title bar. To cover that properly you can use

UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView) {
    titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont boldSystemFontOfSize:20.0];
    titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    titleView.textColor = [UIColor yellowColor]; // Change to desired color
    //titleView.backgroundColor=[UIColor blackColor];
    titleView.text = @"IL APP";
    titleView.textAlignment=UITextAlignmentCenter;
    [self.navigationItem setTitleView:titleView];
    [titleView release];
}
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

For using an image you can use an image view instead of a label to be assigned as the titleView for navigationItem.

Hope it helps

Upvotes: 3

Related Questions