Change navigation bar bottom border color Swift

前端 未结 8 1234
时光取名叫无心
时光取名叫无心 2020-12-30 21:36

It works with

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup a         


        
相关标签:
8条回答
  • 2020-12-30 22:21

    There's a much better option available these days:

    UINavigationBar.appearance().shadowImage = UIImage()
    
    0 讨论(0)
  • 2020-12-30 22:24

    You have to adjust the shadowImage property of the navigation bar.

    Try this one. I created a category on UIColor as an helper, but you can refactor the way you prefer.

    extension UIColor {
        func as1ptImage() -> UIImage {
            UIGraphicsBeginImageContext(CGSizeMake(1, 1))
            let ctx = UIGraphicsGetCurrentContext()
            self.setFill()
            CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1))
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

    Option 1: on a single navigation bar

    And then in your view controller (change the UIColor to what you like):

    // We can use a 1px image with the color we want for the shadow image
    self.navigationController?.navigationBar.shadowImage = UIColor.redColor().as1ptImage()
    
    // We need to replace the navigation bar's background image as well 
    // in order to make the shadowImage appear. We use the same 1px color tecnique
    self.navigationController?.navigationBar.setBackgroundImage(UIColor.yellowColor‌​().as1ptImage(), forBarMetrics: .Default)    
    

    Option 2: using appearance proxy, on all navigation bars

    Instead of setting the background image and shadow image on each navigation bar, it is possible to rely on UIAppearance proxy. You could try to add those lines to your AppDelegate, instead of adding the previous ones in the viewDidLoad.

    // We can use a 1px image with the color we want for the shadow image
    UINavigationBar.appearance().shadowImage = UIColor.redColor().as1ptImage()
    
    // We need to replace the navigation bar's background image as well 
    // in order to make the shadowImage appear. We use the same 1px color technique
    UINavigationBar.appearance().setBackgroundImage(UIColor.yellowColor().as1ptImage(), forBarMetrics: .Default)
    
    0 讨论(0)
  • 2020-12-30 22:26

    Wonderful contributions from @TheoF, @Alessandro and @Pavel.

    Here is what I did for...

    Swift 4

    extension UIColor {
    
        /// Converts this `UIColor` instance to a 1x1 `UIImage` instance and returns it.
        ///
        /// - Returns: `self` as a 1x1 `UIImage`.
        func as1ptImage() -> UIImage {
            UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
            setFill()
            UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

    Using it in viewDidLoad():

    /* In this example, I have a ViewController embedded in a NavigationController in IB. */
    
    // Remove the background color.
    navigationController?.navigationBar.setBackgroundImage(UIColor.clear.as1ptImage(), for: .default)
    
    // Set the shadow color.
    navigationController?.navigationBar.shadowImage = UIColor.gray.as1ptImage()
    
    0 讨论(0)
  • 2020-12-30 22:29

    for Swift 3.0 just change this line:

    CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1))
    

    to this:

    ctx?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
    
    0 讨论(0)
  • 2020-12-30 22:30

    Putting @alessandro-orru's answer in one extension

    extension UINavigationController {
    
        func setNavigationBarBorderColor(_ color:UIColor) {
            self.navigationBar.shadowImage = color.as1ptImage()
        }
    }
    
    extension UIColor {
    
        /// Converts this `UIColor` instance to a 1x1 `UIImage` instance and returns it.
        ///
        /// - Returns: `self` as a 1x1 `UIImage`.
        func as1ptImage() -> UIImage {
            UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
            setFill()
            UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
            UIGraphicsEndImageContext()
            return image
        }
    }
    

    then in your view controller just add:

    self.navigationController?.setNavigationBarBorderColor(UIColor.red)
    
    0 讨论(0)
  • 2020-12-30 22:32

    For iOS 13 and later

        guard let navigationBar = navigationController?.navigationBar else { return }
        navigationBar.isTranslucent = true
        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundImage = UIImage()
            appearance.backgroundColor = .clear
            navigationBar.standardAppearance = appearance
        } else {
            navigationBar.setBackgroundImage(UIImage(), for: .default)
            navigationBar.shadowImage = UIImage()
        }
    
    0 讨论(0)
提交回复
热议问题