UINavigationBar纹理背景

时间:2012-03-30 12:40:32

标签: iphone objective-c ios cocoa quartz-graphics

我看着它,谷歌搜索,自己尝试,我只是想出办法用背景纹理绘制UINavigationBar。

在您开始指出覆盖drawRect setBackgroundImage:forBarMetrics:或任何其他类似方法之前,让我解释一下:

我想要的是绘制一个带有背景纹理的导航栏(样本附加),并保持UINavigationBar提供的半透明渐变效果。从我搜索的内容来看,看起来这样做的唯一方法是在图像本身中包含该效果,但我宁愿使用UINavigationBar效果(动态地,你看到)或者如果没有办法创建UIImageView ,使用Quartz绘制效果并将其添加为UINavigationBar子视图。

你怎么看?有没有办法在Photoshop中绘制效果?

由于

enter image description here

4 个答案:

答案 0 :(得分:2)

您仍然可以尝试将自定义子视图插入UINavigationBar的正确索引中。但是每个iOS版本的代码都会有所不同。您也会松动自动调整大小(旋转时)。但这可能是你走的最简单方法。

更好的是,直接在图像上绘制光泽/反射效果并使用您已经提到的标准方法。示例:iPhone Glossy Icons Using Core Graphics

答案 1 :(得分:2)

回答我自己的问题:

似乎通常的方法是在图像本身上包含闪亮的渐变,但如果我设法通过使用CAGradientLayer绘制渐变来以编程方式进行。

我使用的代码是

    //Create a image view
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];    
    imgView.frame = navBar.bounds;
    imgView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

    //Create a gradient layer
    //Thanks to Kris Johnson's code
    //https://bitbucket.org/KristopherJohnson/gradientbuttons/src/tip/Classes/GradientButton.m    
    CAGradientLayer *shineLayer = [CAGradientLayer layer];
    shineLayer.frame = imgView.bounds;
    shineLayer.colors = [NSArray arrayWithObjects:
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         nil];
    shineLayer.locations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.8f],
                            [NSNumber numberWithFloat:1.0f],
                            nil];


    //Add the gradient layer to the imageView
    [imgView.layer insertSublayer:shineLayer atIndex:1];    

    //Add the imageview to the navbar
    [navBar insertSubview:imgView atIndex:1];

答案 2 :(得分:-1)

试试这个:

   self.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];

答案 3 :(得分:-1)

试试这个:

if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"your_navbar.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
    UIImageView *imageView = imageView = [[UIImageView alloc] initWithImage:
                     [UIImage imageNamed:@"your_navbar.png"]];
    [imageView setTag:1];
    [self.navigationController.navigationBar insertSubview:imageView atIndex:0];
    [imageView release];
}