Home > Enterprise >  How to change Navigation shadow color when changed mode?
How to change Navigation shadow color when changed mode?

Time:12-31

navigationController?.navigationBar.layer.shadowColor = ColorPalette.navigationBarShadowColor.cgColor

Set color according to traits mode.

class ColorPalette : NSObject {

    static var navigationBarShadowColor: UIColor {       
        return UIColor { (traits) -> UIColor in
            //dark: Black // light: grey16
            return traits.userInterfaceStyle == .dark ?
            UIColor.black : UIColor(hex: "dbdbdc")
        }
    }
}

CodePudding user response:

you can create a swift file add the below code in that file.

public enum Mode {
    
    public enum Colors {
        public static var backgroundColor: UIColor = {
            if #available(iOS 13, *) {
                return UIColor { (UITraitCollection: UITraitCollection) -> UIColor in
                    switch UITraitCollection.userInterfaceStyle {
                        case .light, .unspecified:
                            // light mode detected
                            return .blue
                        case .dark:
                            // dark mode detected'
                            return .yellow
                        @unknown default:
                            return .red
                    }
                }
            } else {
                /// Return a fallback color for iOS 12 and lower.
                return .red
            }
        }()
    }
}

after that in your viewController add the below code in the viewWillAppear method

yourView.backgroundColor = Mode.Colors.backgroundColor

CodePudding user response:

Add this in AppDelegate

UINavigationBar.appearance().shadowColor = UITraitCollection.current.userInterfaceStyle == .light ? UIColor(named: "light_color") : UIColor(named: "dark_color")

CodePudding user response:

remove navigationBar shadow

self.navigationController?.navigationBar.shadowImage = UIImage()

if iOS 13

self.navigationController?.navigationBar.standardAppearance.shadowColor = UIColor.black 

CodePudding user response:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
      super.traitCollectionDidChange(previousTraitCollection)
      self.navigationController?.navigationBar.standardAppearance.shadowColor = UIColor.black.cgColor 
  }

we are using this and it is working but we want only one class we have to add this type of code like AppDelegate or some other classes .

  • Related