Question

In my app, I have to customize my navigation bar depending on the page I'm on. In some views, I need to add some UIButton, but also some UIImage (for the logo that is not centered) or some UILabel.

I could do that using [self.navigationController.navigationBar addSubview:myLabel]. But I don't really like this method because if I ever have to add a UIButton or just change the text of the UILabel, I need to change manually the origin of each UILabel and each UIImage.

I thought of using something that already exists : UIBarButtonItem. It can contain some text, an image, and I just need to add each of them in the navigationItem.leftBarButtonItems, and every thing is nicely positioned.

The only problem that remains, is that it still is a button, and reacts as a one when pressed. If I disable it, it's alpha changes to show it's disabled... How could I keep it's normal apparence, and let it disabled ?

Thanks !

PS: I'm open to any other idea to add UIImages or UILabels in a navigation bar :)

Was it helpful?

Solution 2

You can do this by not setting any @selector for created button, and setting the same customized properties for both UIControlStateNormal and UIControlStateSelected. Also, set tintColor to ClearColor. Like that, button will still be a button, it will stay there, always look the same but it won't do anything.

Hope it helps.

OTHER TIPS

Try this code snippet. You can change Label and imageView frame and put as your requirement.

UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];

UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = @"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = @"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];


UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
 imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:@"abc.png"];
 [btn addSubview:imgviw];

self.navigationItem.titleView = btn;

Hope that it helps.

you can use barButton created with customView.

for example,

UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 100, 40)];
[btn setTitle:@"some Title" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor greenColor]];

self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:btn];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top