Remove 1px border under UINavigationBar - not working

ぃ、小莉子 提交于 2020-01-03 08:35:14

问题


@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!