pmor
pmor

Reputation: 6276

Trying to add subview to navigation bar

What I tried:

UIView *view = [[UIView alloc] initWithFrame:frame];
[view setBackgroundColor:[UIColor redColor]];
[[UINavigationBar appearance] addSubview:view];     // does't work

[self.navController.view addSubview:view];          // does't work
[self.navController.view bringSubviewToFront:view]; // 

The question is how we can correctly add a subview to our navigation bar in iOS7? Thanks in advice.

UPD0: Sorry, guys. I got it partly. It was because I have self.navController.navigationBarHidden = YES after setupAppearance. Well, it seems that one face with interesting nav. bar implementation. The navigationBarHidden and in each view we have custom nav. bar. I should dive in details. Anyway thanks for the reply.

UPD1: Just continuing search solution to add custom view to nav. bar like background image.

Upvotes: 6

Views: 13142

Answers (2)

lucianomarisi
lucianomarisi

Reputation: 1552

You have to add it to the navigation bar, try this:

[self.navigationController.navigationBar addSubview:view];

Upvotes: 11

Rudi Angela
Rudi Angela

Reputation: 1473

Here is how I did it: In my main view controller I initialize a UINavigationController with the main view controller as delegate (the main view needs to implement UINavigationControllerDelegate):

self.nav = [[UINavigationController alloc] initWithRootViewController: self];

The navigation controller will automatically take care of creating a UINavigationBar. Also you can add the subviews to the UINavigationBar, via this navigation contoller. Here is how I added the subview:

UIView *buttonsView = [[UIView alloc] initWithFrame:buttonsFrame];
button = [self buttonWithName:@"crop" target:self selector:@selector(cropImage) left:left];
[buttonsView addSubview: button];
...
self.navigationItem.titleView = buttonsView;

That's all.

Upvotes: 1

Related Questions