首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UINavigationBar纹理背景

UINavigationBar纹理背景
EN

Stack Overflow用户
提问于 2012-03-30 12:40:32
回答 4查看 876关注 0票数 2

我看过了,谷歌了一下,尝试了一下,我就是想不出一种方法来画一个带有背景纹理的UINavigationBar。

在您开始指出重写drawRect、drawRect或任何其他类似方法之前,让我解释一下:

我想要的是画一个导航条与我的背景纹理(样本附件),仍然保持半透明梯度效应提供的UINavigationBar。从我搜索的内容来看,实现这个效果的唯一方法似乎是将该效果包含在图像本身中,但是我更愿意使用UINavigationBar效果(动态的,您可以看到),或者如果没有办法创建一个UIImageView,使用Quartz绘制效果并将其添加到UINavigationBar子视图中。

你认为如何?有没有办法在Photoshop中画出效果呢?

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-04-01 23:48:11

回答我自己的问题:

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

我使用的代码是

代码语言:javascript
复制
    //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
EN

Stack Overflow用户

发布于 2012-03-30 12:52:20

您仍然可以尝试在UINavigationBar的正确索引中插入自定义子视图。但是在每个iOS版本上,这段代码都是不同的。此外,您也会松散自动调整大小(当旋转)。但那可能是你最容易去的方法。

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

票数 2
EN

Stack Overflow用户

发布于 2012-03-30 12:50:48

试试这个:

代码语言:javascript
复制
   self.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9943137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档