How to hide UINavigationBar 1px bottom line

后端 未结 30 2132
不知归路
不知归路 2020-11-22 08:58

I have an app that sometimes needs its navigation bar to blend in with the content.

Does anyone know how to get rid of or to change color of this annoying little ba

相关标签:
30条回答
  • 2020-11-22 09:39

    For iOS 9 users, this worked for me. just add this:

    UINavigationBar.appearance().shadowImage = UIImage()
    
    0 讨论(0)
  • 2020-11-22 09:39

    Within AppDelegate, this has globally changed the format of the NavBar:

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().barTintColor = UIColor.redColor()
        UINavigationBar.appearance().translucent = false
        UINavigationBar.appearance().clipsToBounds = false
        UINavigationBar.appearance().backgroundColor = UIColor.redColor()
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
    

    Haven't managed to implement anything different on a specific VC, but this will help 90% of people

    0 讨论(0)
  • 2020-11-22 09:40

    After studying the answer from Serhil, I created a pod UINavigationBar+Addition that can easily hide the hairline.

    #import "UINavigationBar+Addition.h"
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UINavigationBar *navigationBar = self.navigationController.navigationBar;
        [navigationBar hideBottomHairline];
    }
    
    0 讨论(0)
  • 2020-11-22 09:40

    Another option if you want to preserve translucency and you don't want to subclass every UINavigationController in your app:

    #import <objc/runtime.h>
    
    @implementation UINavigationController (NoShadow)
    
    + (void)load {
        Method original = class_getInstanceMethod(self, @selector(viewWillAppear:));
        Method swizzled = class_getInstanceMethod(self, @selector(swizzled_viewWillAppear:));
        method_exchangeImplementations(original, swizzled);
    }
    
    + (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
        if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
            return (UIImageView *)view;
        }
    
        for (UIView *subview in view.subviews) {
            UIImageView *imageView = [self findHairlineImageViewUnder:subview];
            if (imageView) {
                return imageView;
            }
        }
    
        return nil;
    }
    
    - (void)swizzled_viewWillAppear:(BOOL)animated {
        UIImageView *shadow = [UINavigationController findHairlineImageViewUnder:self.navigationBar];
        shadow.hidden = YES;
    
        [self swizzled_viewWillAppear:animated];
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-22 09:40

    Solution in Swift 4.2:

    private func removeHairlineFromNavbar() {
        UINavigationBar.appearance().setBackgroundImage(
            UIImage(),
            for: .any,
            barMetrics: .default)
        UINavigationBar.appearance().shadowImage = UIImage()
    }
    

    Just put this function at the first Viewcontroller and call it in viewdidload

    0 讨论(0)
  • 2020-11-22 09:41

    In Swift 3.0

    Edit your AppDelegate.swift by adding the following code to your application function:

    // Override point for customization after application launch.
    
    // Remove border in navigationBar
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    
    0 讨论(0)
提交回复
热议问题