skip to Main Content
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")
        }
    }
}

4

Answers


  1. Chosen as BEST ANSWER
    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 .


  2. Add this in AppDelegate

    UINavigationBar.appearance().shadowColor = UITraitCollection.current.userInterfaceStyle == .light ? UIColor(named: "light_color") : UIColor(named: "dark_color")
    
    Login or Signup to reply.
  3. remove navigationBar shadow

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

    if iOS 13

    self.navigationController?.navigationBar.standardAppearance.shadowColor = UIColor.black 
    
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search