开发者

iPhone - Customized UINavigation bar along entire stack

I want to set an image and a label at the center of my UINavigationBar, along all my navigation stack.

What I'm currently doing is adding it to my navigation item titleView. The "problem" with this approach is that I have to call this method in the viewDidLoad for each view controller I push to my navigation stack. The other way around is to add the UILable and UIImageView directly to the UINavigationBar, however that why I have to calculate the center myself, and in addition I read that's not the recommended approach. Any Idea how to get what I want ?

My Code:

    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.];
    titleLabe开发者_开发百科l.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];


Found the a nice way to do it :

Registering yourself as the delegate of UINavigationController will let you receive a callback each time a new controller is about to be pushed.

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

Inside that function, getting the viewController and operating on his navigationitem will do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜