Customizing Title in the NavigationBar for a ABPeo

2019-05-07 01:22发布

问题:

I have created an AddressBook kind of application where I display a list of people in a UITableView, when a person is selected an ABUnknownPersonViewController is pushed. From this ABUnknownPersonViewController the user is able (by using the built in functionality) to either add the person to a "New Contact" or "Existing Contact".

This is where my problem is located. I am using a Custom UILabel for the NavigationBar title throughout my application. And I need to be able to do this for the views that are pushed by "Add New Contact" / "Add to Existing Contact" as well.

I solved this for the "Add New Contact" by creating a category for ABNewPersonViewController, but the same approach doesn't work for the "Add to Existing Contact". I guess this might be due to the fact that is it an ABPersonPickerNavigationController that is pushed.

@implementation ABPeoplePickerNavigationController (customTitle)
-(void)viewDidLoad {
    [super viewDidLoad];    
    self.navigationBar.tintColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
}

The change of color for the NavigationBar's tintColor works fine, but I cant find a proper way to access the title. Help that include a working example where you are able to change the title in a ABPeoplePickerNavigationController that is pushed from a ABUnknownPersonViewController would be much appriciated.

This is how the category for ABNewPersonViewController (that works) looks like:

@implementation ABNewPersonViewController (customTitle)
-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
}
-(void)setTitle:(NSString *)theTitle {
    CGRect frame = CGRectMake(0, 0, 45, 45);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:20.0];
    label.shadowColor = [UIColor colorWithWhite:255.0 alpha:1];
    label.shadowOffset = CGSizeMake(1, 1);
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:23/255.0 green:23/255.0 blue:23/255.0 alpha:1];
    self.navigationItem.titleView = label;
    label.text = theTitle;
    [label sizeToFit];
}

@end

回答1:

I use custom labels for this purpose. You can also change font size with this code.

Example:

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 320 / 11)];

label.font = 16.0;

label.textColor = [UIColor blackColor];

label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

label.backgroundColor = [UIColor clearColor];

label.textAlignment = UITextAlignmentCenter;

label.text = @"Your title";

self.navigationItem.titleView = label;

[label release];

Hope this helps.