Question

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.

Was it helpful?

Solution

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

[self.navigationController.navigationBar addSubview:view];

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top