【问题标题】:Customizing Title in the NavigationBar for a ABPeoplePickerNavigationController在导航栏中为 ABPeoplePickerNavigationController 自定义标题
【发布时间】:2011-06-24 16:52:32
【问题描述】:

我已经创建了一个地址簿类型的应用程序,我在 UITableView 中显示人员列表,当一个人被选中时,一个 ABUnknownPersonViewController 被推送。通过这个 ABUnknownPersonViewController,用户可以(通过使用内置功能)将该人添加到“新联系人”或“现有联系人”。

这是我的问题所在。我在整个应用程序中使用自定义 UILabel 作为 NavigationBar 标题。我还需要能够为“添加新联系人”/“添加到现有联系人”推送的视图执行此操作。

我通过为 ABNewPersonViewController 创建类别解决了“添加新联系人”的问题,但同样的方法不适用于“添加到现有联系人”。我想这可能是因为它是一个被推送的 ABPersonPickerNavigationController。

@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];
}

NavigationBar 的 tintColor 的颜色变化工作正常,但我找不到访问标题的正确方法。包含一个工作示例的帮助将非常有用,您可以在其中更改从 ABUnknownPersonViewController 推送的 ABPeoplePickerNavigationController 中的标题。

这是 ABNewPersonViewController 的类别(有效)的样子:

@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

【问题讨论】:

    标签: iphone objective-c uinavigationbar uilabel abaddressbook


    【解决方案1】:

    为此我使用自定义标签。您还可以使用此代码更改字体大小。

    例子:

    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];
    

    希望这会有所帮助。

    【讨论】:

    • 是的,我也创建了这样的自定义 UILabel,因为我试图用我的文本解释“我在整个应用程序中使用自定义 UILabel 作为 NavigationBar 标题。”我的问题是我无法从类别中访问标题以实际将 UILabel 设置为 NavigationBar 的标题。
    • 我编辑了我的帖子,希望能让事情更清楚,不过感谢反馈。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2011-09-04
    相关资源
    最近更新 更多