Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make the iPhone status bar transparent in Swift?

I am looking to hide the navigation and status bar to make it transparent. I have an image right below that I want to take up the entire space. So far, I got to hide the navigation bar but the status bar is still white- this is in the viewDidLoad():

self.navigationController?.setNavigationBarHidden(true, animated: true)

Also, how can I re add the back button to the the navigation bar?

Thanks!

Screen shot of my current iPhone simulator:

enter image description here

Screen shot after add Mohy Gh's code

Edited after Mohy Gh's Code:

like image 602
Sam Avatar asked Oct 17 '22 08:10

Sam


1 Answers

Swift 3: for whole app:

do this in your AppDelegate's didFinishLaunchingWithOptions method instead of hiding the navigation bar:

// Sets navigationBar's background to a blank/empty image       
UINavigationBar.appearance().setBackgroundImage(UIImage(),
                                                for: .default)

// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = true

EDIT : to hide navigationBar for one specific view controller in your View controller do like this :

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let navigationBar = self.navigationController?.navigationBar

    navigationBar?.setBackgroundImage(UIImage(), for: .default)
    navigationBar?.shadowImage = UIImage()
    navigationBar?.isTranslucent = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    let navigationBar = self.navigationController?.navigationBar

    navigationBar?.shadowImage = nil
    navigationBar?.setBackgroundImage(nil, for: .default)
    navigationBar?.isTranslucent = false
}

After making the navigationBar transparent, you need to set your imageView 's top constraint to topLayoutGuid = 0

if you you want to also hide statusBar for one viewController also put this in your desired viewController:

override var prefersStatusBarHidden: Bool {
    return true
}
like image 68
MohyG Avatar answered Nov 15 '22 06:11

MohyG