7

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
  • 387
  • 2
  • 6
  • 16
  • @AbhishekMitra not completely, the issue you're referencing is using `.appearance` which is used for the whole app. In this case, it's for a single `ViewController`. – Ethenyl Jan 31 '18 at 14:55

2 Answers2

9

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.

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
  • I've already tried this. It doesn't help – bolt Jan 31 '18 at 20:30
  • I get back the back button but the bar is still white – bolt Feb 01 '18 at 13:46
  • @ДмитрийМеньшиков I think that because of your `navigationController?.navigationBar.tintColor = .white` this peace of line, do this `navigationController?.navigationBar.tintColor = .clear` and let me know. – Abhishek Mitra Feb 01 '18 at 13:48
  • By changing this the back button disappears again, and nav bar is still white – bolt Feb 01 '18 at 13:51
  • I've already tried to extend image in storyboard, but the result is the same – bolt Feb 01 '18 at 13:53
  • @ДмитрийМеньшиков my mistake `navigationController?.navigationBar.isTranslucent = true` this must be true. I tried my self and working perfectly.. – Abhishek Mitra Feb 01 '18 at 14:05
  • @ДмитрийМеньшиков Use that code in `override func viewDidAppear(_ animated: Bool)` – Abhishek Mitra Feb 01 '18 at 14:06
  • Still get white nav bar with a back button. I'll try to share my Xcode project in a few minutes – bolt Feb 01 '18 at 14:07
  • @ДмитрийМеньшиков Block your all code what you have got from here and do my new update 2 in answer. Thanks – Abhishek Mitra Feb 01 '18 at 14:09
  • I just get red letters. Nothing else changes – bolt Feb 01 '18 at 14:10
  • @ДмитрийМеньшиков yes, it is for tint color. anyway let me share that project with you. it is working fine. – Abhishek Mitra Feb 01 '18 at 14:11
  • @ДмитрийМеньшиков here you go to have the project: https://drive.google.com/file/d/1DIsqlbbRclC3ViixnL0RQSp95FMeq--a/view?usp=sharing – Abhishek Mitra Feb 01 '18 at 14:13
  • Ok My project https://drive.google.com/drive/folders/130rTCkGhx89fQst_HLDj5qQuFqX1j5_C – bolt Feb 01 '18 at 14:26
  • 2
    @ДмитрийМеньшиков 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. – Chetan Rajagiri Oct 04 '18 at 10:27
  • "Update 2" did the trick on ios 12 swift 4 for iphone XR. – papesky Mar 18 '19 at 18:17
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     
    }
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56