Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an image and a label to UINavigationBar as a title?

For the moment I just know how to put a string as a title with:

[self setTitle:@"iOS CONTROL"];

or an image with

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

but not both in the same time

there is a solution ? Thank you

like image 512
The Fonz Avatar asked Mar 04 '14 12:03

The Fonz


2 Answers

You can create a UIView and add as many subviews as you like. For example:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 40.0)];
titleView.backgroundColor = [UIColor clearColor];

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor redColor];
titleLabel.text = @"My Title Label";
[titleLabel sizeToFit];

...

// Set the frames for imageView and titleLabel to whatever you need them to be

...

[titleView addSubview:imageView];
[titleView addSubview:titleLabel];

self.navigationItem.titleView = titleView;
like image 86
Vinny Coyne Avatar answered Nov 06 '22 03:11

Vinny Coyne


Create a new UIView, add UIImage and a UILabel as subviews to it the way you want and add that UIView using self.navigationItem.titleView.

like image 1
Segev Avatar answered Nov 06 '22 04:11

Segev