【发布时间】:2010-08-23 17:33:55
【问题描述】:
我已经覆盖了 UINavigationBar 上的 drawLayer:inContext: 类别,以便我可以将自定义图像放在那里。我讨厌它,但我的客户很固执。
因此,我不得不退回到一些非常丑陋的 hack 才能把事情搞定。我必须在该类别中放置一个新方法来在导航栏标题上放置标题,并且我必须在-viewDidAppear 中明确调用它。
我可以忍受。真正的问题是,当内存变低时,UINavigationBar 似乎正在卸载它的子视图,包括我的后退按钮!我真的不知道如何从中恢复过来。
这是我的 UINavigationBar 类别。我认为,我从这里得到了这段代码;核心图形大多只是让我头晕目眩。
@implementation UINavigationBar (background)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if([self isMemberOfClass: [UINavigationBar class]]){
UIImage *image = [UIImage imageNamed:@"top_bar.png"];
//CGContextClip(ctx);
CGContextTranslateCTM(ctx, 0, image.size.height - 20);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx, CGRectMake(0, 2, self.frame.size.width, self.frame.size.height), image.CGImage);
}
}
-(void)customTitle:(NSString *)text
{
UINavigationItem *top = self.topItem;
[[top.titleView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
title.textAlignment = UITextAlignmentCenter;
title.text = text;
title.textColor = [UIColor blackColor];
title.font = [UIFont boldSystemFontOfSize:16];
title.backgroundColor = [UIColor clearColor];
top.titleView = title;
[title release];
}
有没有更简单的方法?如果没有,我怎么能告诉 UINavigationBar,“用你应该拥有的子视图重新加载自己”?
更新:尝试改写 -drawRect。代码更简单,但问题仍然存在——在某些情况下,后退按钮消失了。顺便说一句,我假设这与收到低内存警告有关。它似乎只发生在通过 UIImagePicker 的旅行回来之后,所以也许那里发生了一些事情来改变事情。我也确实启用了 NSZombies,显然关闭它会显着改变记忆肤色。
【问题讨论】:
标签: iphone cocoa-touch core-graphics uinavigationbar