2011-05-04 49 views
0

我想在我的UINavigationBar中心沿着我的所有导航堆栈设置图像和标签。iPhone - 沿整个堆栈定制的UINavigation栏

我目前正在做的是将它添加到我的导航项目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]; 

回答

0

找到了一个很好的方式做到这一点:

注册自己作为的UINavigationController的代表将让你得到一个回调每次新的控制器即将推。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 

内部的功能,得到了viewController和他的navigationitem操作就可以了。