Set Image Underlay of Transparent Navigation Bar and Status Bar in Swift iOS 8

前端 未结 4 805
遥遥无期
遥遥无期 2020-12-13 16:42

I am new in iOS swift development and I am facing a problem. I want to set transparent navigation bar and make image underlay of transparent navigation bar and status bar li

相关标签:
4条回答
  • 2020-12-13 16:48

    If you are not using the default navigation bar, then shift your background image view (which is going to visible below the status bar) 20px up from the top constraint, then clear your status bar background color using:

     override func viewDidLoad() {
        super.viewDidLoad()
    
        let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = UIColor.clear
    }
    

    If you want to change the status bar item's color to white then use:

     override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    The output will be

    0 讨论(0)
  • 2020-12-13 16:58

    I solved this by setting transparent UIColor for status bar background.

        guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
                    return
                }
                statusBar.backgroundColor = UIColor(red: 2, green: 200.0, blue: 200, alpha: 0) // color value has no effect. Only alpha value is needed to make it transparent
    
    0 讨论(0)
  • 2020-12-13 17:04

    I have tried same code as you provided:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
        self.navigationController!.navigationBar.shadowImage = UIImage()
        self.navigationController!.navigationBar.translucent = true
    }
    

    And it is working fine and you can see result here:

    enter image description here

    Check my sample project and find out what are you missing.

    Hope it will help.

    0 讨论(0)
  • 2020-12-13 17:11

    As per Dharmesh's answer, but updated for Swift 4

        self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController!.navigationBar.shadowImage = UIImage()
        self.navigationController!.navigationBar.isTranslucent = true
    
    0 讨论(0)
提交回复
热议问题