iPhone-沿整个堆栈自定义导航条

iPhone-沿整个堆栈自定义导航条,iphone,objective-c,ios4,uinavigationbar,uinavigationitem,Iphone,Objective C,Ios4,Uinavigationbar,Uinavigationitem,我想在UINavigationBar的中心设置一个图像和一个标签,沿着我的所有导航堆栈 我目前正在做的是将其添加到我的导航项titleView中。 这种方法的问题是,对于推送到导航堆栈的每个视图控制器,我必须在viewDidLoad中调用此方法。 另一种方法是将UILable和UIImageView直接添加到UINavigationBar,但这就是为什么我必须自己计算中心的原因,此外,我认为这不是推荐的方法。 知道如何得到我想要的吗 我的代码: CGRect navTitle = co

我想在UINavigationBar的中心设置一个图像和一个标签,沿着我的所有导航堆栈

我目前正在做的是将其添加到我的导航项titleView中。 这种方法的问题是,对于推送到导航堆栈的每个视图控制器,我必须在viewDidLoad中调用此方法。 另一种方法是将UILable和UIImageView直接添加到UINavigationBar,但这就是为什么我必须自己计算中心的原因,此外,我认为这不是推荐的方法。 知道如何得到我想要的吗

我的代码:

    CGRect  navTitle = controller.navigationController.navigationBar.bounds;
    CGFloat aHeight = navTitle.size.height;

    UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, aHeight)];


    UIImage* statusImg = [UIUtils   getStatusImage]; 

    UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,aHeight/2-statusImg.size.height/2, 33., 32.)];
    statusImage.autoresizingMask = UIViewAutoresizingNone;
    statusImage.image = statusImg;
    statusImage.backgroundColor = [UIColor clearColor];
    [statusImage setTag:1];
    [statusImage setHidden:NO];

initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(statusImage.frame.origin.x + 33. + 3, 0, 130., navTitle.size.height)];
    titleLabel.textAlignment = UITextAlignmentLeft;
    titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20.];
    titleLabel.shadowOffset = CGSizeMake(1, -1);
    titleLabel.opaque = NO;
    titleLabel.backgroundColor = [UIColor clearColor];
    [titleLabel setTag:2];

    [container addSubview:statusImage];
    [container addSubview:titleLabel];
    controller.navigationItem.titleView = container;
[statusImage release];
[titleLabel release];
    [container release];

找到了一个很好的方法:

将自己注册为UINavigationController的代理将允许您在每次即将推送新控制器时收到回调

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
在这个函数中,获取viewController并操作他的navigationitem就可以了