代码之家  ›  专栏  ›  技术社区  ›  Mark

iPhone UINavigationBar定制外观

  •  2
  • Mark  · 技术社区  · 14 年前

    UINavigationBar 在我的iPhone应用程序上,我多次遇到相同的解决方案,如下所示:

    @implementation UINavigationBar (UINavigationBarCategory) {
        - (void)drawRect:(CGRect)rect {
             UIImage *img = [UIImage imageNamed: @"logo_bar.png"];
             CGContextRef context = UIGraphicsGetCurrentContext();
             CGContextDrawImage(context, CGRectMake(0, 0, 317, 27), img.CGImage);
    }
    @end
    

    在一定程度上是可行的

    我的形象是 上下颠倒 !! 那到底是为什么?我已经检查了图像本身,这是好的。这种转变将在哪里得到应用??

    我还有其他问题,比如:

    1. 如何选择显示BG图像或在选定屏幕上切换其alpha?有一个特殊的屏幕上有一个后退按钮,它涵盖了图像,我不想,我宁愿只是不显示在屏幕上的图像。

    2. 图像不是整个屏幕的高度 导航栏

    谢谢,我以前没有使用过类别,这可能是我理解这一点的关键,但也许不是。。。

    2 回复  |  直到 14 年前
        1
  •  2
  •   andi1492    14 年前
    // Drawing code
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    UIImage *back=[UIImage imageNamed:@"bar.png"];
    
    CGContextScaleCTM(c,1,-1);
    CGContextTranslateCTM(c,0,-44);
    
    CGContextDrawImage(c,CGRectMake(0, 0, 320, 44),[back CGImage]);
    

    这是我如何绘制我的自定义导航栏。希望有帮助。

        2
  •  0
  •   Ahmet Ardal    14 年前

    你也可以在 拖拉ayer:inContext: UINavigationBar类别类中的方法。内部 拖拉ayer:inContext:

    - (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
    {
        if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
            return;
        }
    
        UIImage *image = (self.frame.size.width > 320) ?
                            [UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
        CGContextClip(context);
        CGContextTranslateCTM(context, 0, image.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
    }
    

    以及 this complete demo Xcode project