首页 文章

iOS 11导航栏透明,带有大 Headers

提问于
浏览
3

我有uinavigationbar的默认首选项:

UINavigationBar.appearance().barTintColor = .red
        UINavigationBar.appearance().titleTextAttributes = [ NSAttributedStringKey.foregroundColor:#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)]
        if #available(iOS 11.0, *) {
            UINavigationBar.appearance().largeTitleTextAttributes = [ NSAttributedStringKey.foregroundColor:#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)]
        }
        UINavigationBar.appearance().isTranslucent = false
        UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)

结构: UINaviagtionController -> PostsTableViewController -> PostTableViewController .

我想在PostViewView上使用PostsViewController上的大导航栏(具有不透明的背景颜色)和透明导航栏 .

在PostTableViewController中我添加:

self.navigationController?.navigationBar.backgroundColor = UIColor.clear
        self.navigationController?.navigationBar.barTintColor = .clear

        self.automaticallyAdjustsScrollViewInsets = false
        //self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        //self.navigationController?.navigationBar.shadowImage = UIImage()
        if #available(iOS 11.0, *) {
            print("asd")
            self.tableView.insetsContentViewsToSafeArea = false
            self.tableView.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }

导航栏是黑色的 . 我不明白如何使它透明...
PostsTableViewController

PostTableViewController

UPDATE:

git hub存储库:https://github.com/Mazorati/testnavbar

我设置isTranslucent = true,但大导航也变得透明 . 只有默认导航才行 .

PostsViewController

PostsViewController

PostViewController

但我希望导航栏颜色不透明,所以:

self.navigationController?.view.backgroundColor = .red
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .automatic

作为结果:

opaque large navigation

3D图层:

3d layers

2 回答

  • 0

    试试这个,这会让你的导航栏变得透明 .

    let img = UIImage()
        navigationController?.navigationBar.shadowImage = img
        navigationController?.navigationBar.setBackgroundImage(img, for: .default)
    
  • 0

    你试过把它变成半透明的吗?

    self.navigationController?.navigationBar.isTranslucent = true
    

    更新:

    您必须记住,导航栏属于UINavigationController,它将视图保存在其中,因此当您将其更改为一个时,它将全部更改 .

    解决方案是在第二个视图控制器的viewWillAppear方法中将其更改为半透明,并在viewDidDissapear方法中将其更改为不透明 . 使用viewWill和viewDid播放方法,直到找到适合您需要的方法 .

相关问题