问题
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:
Shows a faded gray color view through iOS 7 Navigation bar of a UISplitViewController
Any idea will be appreciated.
EDIT 6 oct 2015 : If somebody needs to know how to perform that
For Swift users : - in particular viewcontroller : i use :
self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: "fadedimage.png"), forBarMetrics: .Default)
in the main NavigationController (to apply that to the all navigationBar) i use :
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "fadedimage.png"), forBarMetrics: .Default)
回答1:
This might not be the correct way, but you can achieve the same by using a custom image.
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]];
This gives the result:
Later you can apply the shadow effect for the navigation bar.
Edit:
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.
Here is the image I used:
To remove the bottom border that appears on navbar, use this code:
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
来源:https://stackoverflow.com/questions/32955102/set-a-faded-darker-background-to-a-uinavigationbar