问题
@IBOutlet var navBar: UINavigationBar!
self.navBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navBar.shadowImage = UIImage()
The code above works if the View Controller is embedded in a Navigation Controller and uses self.navigationController?.navigationBar.setBack... etc, but it doesn't work when using an IBOutlet (my example is not embedded in nav controller). The navigation bar is not translucent.
Any ideas?
回答1:
Put this in you view controller (not in the uinavigationbar).
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
回答2:
Where exactly are you calling the appearance settings for a NavBar? When I try the following:
[self.myNC.navigationBar setShadowImage:[UIImage new]];
[self.myNC.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
I have the desired effect.Could you provide us with a little more context perhaps?
回答3:
If you use a nav bar without nav view controller, you can try to set navigationBar.clipsToBounds = YES. I have a separate UIToolbar and this approach works now.
回答4:
This works fine with an IBOutlet for the navigation bar.
@IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationBar.shadowImage = UIImage()
}
Double check that your navigation bar is set as a referencing outlet to the view controller.
Here is a sample project showing that it works as expected: http://bjtitus.s3.amazonaws.com/NavBarBorderTest.zip
来源:https://stackoverflow.com/questions/27254825/remove-1px-border-under-uinavigationbar-not-working