Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

patrick tagliaferro
patrick tagliaferro
5,399 Points

Custom nav bar with UIImagePickerControl

I have replaced the nave bar in my iOS7 app in Xcode 5 using the following code (also removed the status bar if that matters):

AppDelegate.M

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault];

In one of my image view controllers I am using a UIImagePicker to pick a profile pic. The problem is when it goes to the photo gallery it uses the main "navBar" image which hides some controls and basically stacks 2 of the same nav bars on top. Can I have a different nav bar for the photogallery? Here is the View controllers:

FirstView.H

#import <UIKit/UIKit.h>

@interface IBFirstViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *userEmail;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImage;


- (IBAction)logout:(id)sender;
- (IBAction)edit:(id)sender;
@end

FirstView.M

- (IBAction)edit:(id)sender {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:NO completion:Nil];
}

1 Answer

Stone Preston
Stone Preston
42,016 Points

So you are saying the imagePicker controller is using the image you set in your app delegate correct? If you wanted to change the image just for that imagePickers bar, maybe you could try using the following delegate method:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isKindOfClass:[UIImagePickerController class]] {

    //customize your navigationController here
    [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"]    forBarMetrics:UIBarMetricsDefault];

    }
}

and then maybe change the image back when the imagePicker controller is dismissed?