Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 581
bolt Avatar asked Jan 31 '18 13:01

bolt


People also ask

How do you make the navigation bar transparent in Swift IOS?

You need to do three things to make a navigation bar transparent. Set background image to non-nil empty image ( UIImage() ). Set shadow image to non-nil empty image ( UIImage() ). Set isTranslucent to true .

How do you make the navigation bar transparent?

Creating a transparent navbar is very easy - just don't add a color class . bg-* to the navbar. In this case, the Navbar will take the color of the parent's background color.


1 Answers

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.

like image 186
Abhishek Mitra Avatar answered Nov 15 '22 00:11

Abhishek Mitra