如何设置透明的导航栏?iOS 11 Swift 4 Xcode 9

Дми*_*ков 4 xcode ios swift swift4 ios11

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

1个

我正在尝试制作一个透明的导航栏,在我读的这本书中,您所要做的就是将这段代码插入首选的View Controller的viewDidLoad()方法中:

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

tableView.contentInsetAdjustmentBehavior = .never
Run Code Online (Sandbox Code Playgroud)

但是我得到的只是一个白色的导航栏。另外,如果写出图片上条形的差异在此代码中:

tableView.contentInsetAdjustmentBehavior = .never
Run Code Online (Sandbox Code Playgroud)

但这对我不起作用

我下载了本书这一章的最后一个项目,并且在那儿一切正常,尽管我试图复制粘贴代码,但仍然没有任何改变。

问题是-我已经尝试插入以下代码:

navigationController?.navigationBar.isTranslucent = true
Run Code Online (Sandbox Code Playgroud)

但这不起作用

如果重要的话,这本书是AppCoda撰写的“ iOS 11编程入门”。

Abh*_*tra 6

使用以下代码:

navigationController?.navigationBar.isTranslucent = true
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助。

编辑(更新)

使用以下代码:

navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = false
Run Code Online (Sandbox Code Playgroud)

更新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
    }
Run Code Online (Sandbox Code Playgroud)

必须要工作。

  • @ДмитрийМеньшиков确保要添加的最上面的视图,不应将顶部约束设置为要查看的安全区域。然后上面的代码工作正常。 (2认同)