MichiZH
MichiZH

Reputation: 5817

iOS 8: Cannot change the navigation bar at all of the ABPeoplePickerNavigationController

I try to change the color of the ABPeoplePickerNavigationController navbar to my app colors. However no changes occur. Even when I try to hide it or at least change the status bar tint color, nothing happens. Why?

- (ABPeoplePickerNavigationController *)peoplePicker
{
    if(!_peoplePicker){
        _peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
        _peoplePicker.peoplePickerDelegate = self;
        _peoplePicker.navigationBar.hidden = YES;
    }
    return _peoplePicker;
}

I lazy instantiate my navigation controller and the other method calls like dismiss view controller etc. work fine.

EDIT: This is my current code (for the bounty). No color changes happen like this:

- (ABPeoplePickerNavigationController *)peoplePicker
{
    if(!_peoplePicker){
        _peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
        _peoplePicker.peoplePickerDelegate = self;
        _peoplePicker.navigationBar.tintColor = [UIColor blackColor];
    }
    return _peoplePicker;
}

- (IBAction)addressBookButtonClicked:(id)sender {
    [self presentViewController:self.peoplePicker animated:YES completion:nil];
}

Doesn't this work anymore on iOS8?

Upvotes: 4

Views: 2299

Answers (4)

Ilker Baltaci
Ilker Baltaci

Reputation: 11787

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    [[UIBarButtonItem appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setBarTintColor:[UIColor redColor]];
    [picker.navigationController.navigationBar setTranslucent:NO];

I call these methods before presenting the person picker not even in the viewdidload.

Upvotes: 2

Keaton Burleson
Keaton Burleson

Reputation: 508

Actually, thanks to the comment above, I realized that an appearance proxy should work like so:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

Just put that in the viewDidLoad method of the View Controller from which you are instigating the ABPeoplePickerView. This will also work for MFMailerView as well. My code that I'm using to style my bars properly is:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
                                                                       fontWithName:@"Helvetica Neue" size:12], NSFontAttributeName,
                            [UIColor whiteColor], NSForegroundColorAttributeName, nil];

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

Upvotes: 7

Reinhard Männer
Reinhard Männer

Reputation: 15247

You are using an instance variable (iVar) _peoplePicker, which belongs apparently to a property peoplePicker. Additionally, you have a method peoplePicker that returns an object of class ABPeoplePickerNavigationController *.
In your IBAction addressBookButtonClicked: you execute the instruction

[self presentViewController:self.peoplePicker animated:YES completion:nil];

This means you call your method peoplePicker, which checks the iVar _peoplePicker and sets it (e.g. the tint color) if it has a nil value.
But since you are using the iVar directly, i.e. _peoplePicker instead of self.peoplePicker, you do not use the setter method, but simply an assignment.
This means the retain count of the UIColor object that you create in [UIColor blackColor] with an retain count of 1 will not be increased by the instruction _peoplePicker.navigationBar.tintColor = [UIColor blackColor]; but will be put into an autorelease pool when it is returned by return _peoplePicker;. But the autoreleasepool will be drained when you app returns to the main event loop, and the UIColor object will be released.
So I suggest that you replace all assignments to _peoplePicker by assignments to self.peoplePicker. Then you are using the setter method, which will increase the retain count, and your UIColor object will survive.

Upvotes: 1

fabianfett
fabianfett

Reputation: 729

the color issue: The navigationBar might not be loaded at the point when you are trying to access it. Have you tried to set the color properties after presenting the ABPeoplePickerNavigationController.

the hide issue: Normally you hide the NavigationBar by calling:

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;

on UINavigationController, in your case ABPeoplePickerNavigationController.

Cheers, Fabian

Upvotes: 1

Related Questions