UINavigationBar set custom shadow in AppDelegate.swift

前端 未结 2 2030
庸人自扰
庸人自扰 2021-01-13 02:24

I want to set some shadow to the bottom of my UINavigationBar for the whole application. Here is what I\'ve tried but it doesn\'t work:

func application(appl         


        
2条回答
  •  时光取名叫无心
    2021-01-13 02:50

    A better solution by still using appearance and that does not require you to subclass the UINavigationBar and adding code to each navigation controller, would be:

    Extend the UINavigationBar

    extension UINavigationBar {
    
      var castShadow : String {
        get { return "anything fake" }
        set {
            self.layer.shadowOffset = CGSizeMake(0, 3)
            self.layer.shadowRadius = 3.0
            self.layer.shadowColor = UIColor.yellowColor().CGColor
            self.layer.shadowOpacity = 0.7
    
        }
      }
    
    }
    

    And add an app wide appearance rule (inside appdelegate "didFinishLaunchingWithOptions" for example)

    UINavigationBar.appearance().castShadow = ""
    

提交回复
热议问题