【问题标题】:Memory warning removes UINavigationBar buttons内存警告删除 UINavigationBar 按钮
【发布时间】: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


    【解决方案1】:

    为什么不在 UINavigationBar 类别中覆盖 drawRect,而不是 drawLayer:inContext:?

    据我所知,这是一个非常干净的 hack,无需为您的标题提供自定义 UILabel。

    更新:您关于内存警告的信息更有意义。您是否设置了自定义后退按钮?当控制器收到内存警告时,如果它不在屏幕上,它会放弃它的视图。您的导航控制器很有可能会发生这种情况。如果您在 init 方法中设置自定义后退按钮,则无法将其重新添加到导航项后内存警告清除中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多