如何设置透明导航栏?iOS 11 快速 4 Xcode 9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48543394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:32:04  来源:igfitidea点击:

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

iosswiftxcodeios11swift4

提问by Aphro Disiac

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

右边的图片是我需要的,左边是我得到的:

1

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:

我正在尝试制作一个透明的导航栏,在我正在阅读的书中写道,您需要做的就是将此代码插入到首选视图控制器的 viewDidLoad() 方法中:

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

如果重要的话,这本书是 AppCoda 的“开始 iOS 11 编程”

回答by Abhishek Mitra

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

更新 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.

它必须是工作。

回答by Khawar Islam

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     
    }