将褪色的深色背景设置为UINavigationBar [英] Set a faded darker background to a UINavigationBar
问题描述
我正在尝试为导航栏设置一个较暗的深色背景,如下图所示.
I'm trying to set a faded darker background to a navigation bar, as you can see on the image below.
我已经尝试过使用此代码,但这并不是我所期望的.
I've tried with this code but it's not exactly what I expect.
UINavigationBar.appearance().backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
我找到了这个主题,但是没有合适的答案:
I've found this topic, but without suitable answer:
显示通过UISplitViewController的iOS 7导航栏淡化灰色视图
任何想法将不胜感激.
Any idea will be appreciated.
EDIT 2015年10月6日:如果有人需要知道如何执行
EDIT 6 oct 2015 : If somebody needs to know how to perform that
对于Swift用户: -特别是viewcontroller:我使用:
For Swift users : - in particular viewcontroller : i use :
self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: "fadedimage.png"), forBarMetrics: .Default)
在主NavigationController中(将其应用于所有navigationBar),我使用:
in the main NavigationController (to apply that to the all navigationBar) i use :
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "fadedimage.png"), forBarMetrics: .Default)
推荐答案
这可能不是正确的方法,但是您可以通过使用自定义图像来实现相同的目的.
This might not be the correct way, but you can achieve the same by using a custom image.
从互联网上下载透明图像,我将其命名为transparent.png
Donwload a transparent image from internet, I named it as transparent.png
并设置导航栏背景图片:
and set the navigation bar background image:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"transaprent.png" ] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
这给出了结果:
稍后您可以为导航栏应用阴影效果.
Later you can apply the shadow effect for the navigation bar.
我下载了一个自定义的阴影透明png图像,并使用上面的代码对其进行了应用.
I donwloaded a custom shadow transparent png image, and applied it using above code.
您仍然可以根据需要使用更好的自定义图像来改进此功能.
You can still improve this with a better custom image as per your requirements.
这是我使用的图像:
要删除导航栏上显示的底部边框,请使用以下代码:
To remove the bottom border that appears on navbar, use this code:
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
这篇关于将褪色的深色背景设置为UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!