删除导航栏底部边框iOS 7

时间:2013-09-27 09:27:47

标签: objective-c uinavigationbar ios7 xcode5

有没有办法删除iOS7自动显示在导航栏下方的底部边框?

9 个答案:

答案 0 :(得分:48)

这在带有半透明导航的iOS7上不起作用......

来自Apple文档的粘贴;

  

描述   用于导航栏的阴影图像。   默认值为nil,对应于默认阴影图像。当非零时,此属性表示要显示的自定义阴影图像,而不是默认值。要显示自定义阴影图像,还必须使用setBackgroundImage:forBarMetrics:方法设置自定义背景图像。如果使用默认背景图像,则无论此属性的值如何,都将使用默认阴影图像。

所以基本上你需要实现那个setBackgroundImage。 附加说明,在iOS7上您将不再使用外观,但您将在viewController上下文中修改导航栏。

那是:

    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];

在我的情况下,我把它放在viewDidLoad中(可以为UINavigationViewController中的每个UIViewController添加自定义行为。)

答案 1 :(得分:46)

如果我理解你正确的尝试

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

答案 2 :(得分:12)

基于muffed2k的答案+编程托马斯评论, 这就是我用来显示没有背景图像(ios5.1 / 6.0)和没有底边框(ios7.0)的UINavigationBar:

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6)
    {
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }else
    {
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }

答案 3 :(得分:6)

如果您正在使用Swift并遇到此问题,请在主ViewController中尝试此操作:

override func viewDidLoad() {
    super.viewDidLoad()

    /// ...

    navigationController?.navigationBar.shadowImage = UIImage();
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)

    //...
}

基于@ wolffan的上述答案

答案 4 :(得分:4)

对于我来说,当translucent设置为false

时,我在iOS 7到9+上工作过
UINavigationBar.appearance().transluscent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics:.Default)

答案 5 :(得分:2)

我知道已经有一个已接受的答案,但另一种方法是将clipToBounds设置为true。

以下是在swift中执行此操作的一行代码

self.navigationController?.navigationBar.clipsToBounds = true

为我工作就像一个魅力。

答案 6 :(得分:1)

for objective c

self.navigationController.navigationBar.clipsToBounds = YES;

答案 7 :(得分:1)

像魅力一样: Swift 3.x版

    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

答案 8 :(得分:0)

如果您的目标是iOS 7并且 设置背景图片,那么这将有效:

        CGFloat navigationBarWidth = self.navigationController.navigationBar.frame.size.width;
        CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
        CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(navigationBarWidth, navigationBarHeight + statusBarHeight), NO, 0.0);
        UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];

        //the following line takes away the border but only works if a background image is set (above)
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

我从@ muffe2k的回答和this SO post得到了这个想法。