How to set a transparent navigation bar? iOS 11 swift 4 Xcode 9

12,282

Solution 1

Use following code:

navigationController?.navigationBar.isTranslucent = true

Hope it will help you.

Edit (UPDATE)

Use Below Code:

navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = false

UPDATE 2

override func viewDidAppear(_ animated: Bool) {

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

It have to be work.

Solution 2

Check this code

  override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true
    }
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = false     
    }
Share:
12,282
bolt
Author by

bolt

Updated on June 06, 2022

Comments

  • bolt
    bolt almost 2 years

    On the picture on the right is what I need and on the left is what I get:

    1

    I'm trying to make a transparent navigation bar, and in the book which I'm reading it's written that all you need to do is to insert this code in viewDidLoad() method of the preferable View Controller:

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.tintColor = .white
    
    tableView.contentInsetAdjustmentBehavior = .never
    

    But all I get is a white navigation bar. Also if's written that the difference of bars on the picture is in this code:

    tableView.contentInsetAdjustmentBehavior = .never
    

    But it doesn't work for me

    I downloaded the final project of this book's chapter and everything works fine there, though I've tried to copy-paste the code and still got nothing changed

    And the thing is - I've already tried to insert this code:

    navigationController?.navigationBar.isTranslucent = true
    

    But it doesn't work

    If it matters, the book is "Beginning iOS 11 programming" by AppCoda

  • bolt
    bolt over 6 years
    I've already tried this. It doesn't help
  • bolt
    bolt over 6 years
    I get back the back button but the bar is still white
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @ДмитрийМеньшиков I think that because of your navigationController?.navigationBar.tintColor = .white this peace of line, do this navigationController?.navigationBar.tintColor = .clear and let me know.
  • bolt
    bolt over 6 years
    By changing this the back button disappears again, and nav bar is still white
  • bolt
    bolt over 6 years
    I've already tried to extend image in storyboard, but the result is the same
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @ДмитрийМеньшиков my mistake navigationController?.navigationBar.isTranslucent = true this must be true. I tried my self and working perfectly..
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @ДмитрийМеньшиков Use that code in override func viewDidAppear(_ animated: Bool)
  • bolt
    bolt over 6 years
    Still get white nav bar with a back button. I'll try to share my Xcode project in a few minutes
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @ДмитрийМеньшиков Block your all code what you have got from here and do my new update 2 in answer. Thanks
  • bolt
    bolt over 6 years
    I just get red letters. Nothing else changes
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @ДмитрийМеньшиков yes, it is for tint color. anyway let me share that project with you. it is working fine.
  • Abhishek Mitra
    Abhishek Mitra over 6 years
    @ДмитрийМеньшиков here you go to have the project: drive.google.com/file/d/1DIsqlbbRclC3ViixnL0RQSp95FMeq--a/…
  • bolt
    bolt over 6 years
  • Khawar Islam
    Khawar Islam over 6 years
    i download your project but main.stroyboard is missing and some other files are missing
  • Chetan Rajagiri
    Chetan Rajagiri over 5 years
    @ДмитрийМеньшиков make sure that the top most view you are adding, the top constraint shouldn't be set to safe area it should be set to view. Then the above code works fine.
  • papesky
    papesky about 5 years
    "Update 2" did the trick on ios 12 swift 4 for iphone XR.