簡體   English   中英

iOS 11導航欄透明,帶有大標題

[英]iOS 11 navigation bar transparent with large titles

我對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

我想在PostsViewController上使用大型導航欄(背景顏色不透明),在PostViewController上使用透明導航欄。

在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

更新:

git中心存儲庫: https : //github.com/Mazorati/testnavbar

我設置isTranslucent = true,但是大型導航也變得透明。 只有默認導航是可以的。

PostsViewController

PostsViewController

PostViewController

但我希望Navigationbar顏色不透明,所以:

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

作為結果:

不透明的大型導航

3D層:

3d圖層

您是否嘗試過將其更改為半透明?

self.navigationController?.navigationBar.isTranslucent = true

更新:

您必須記住,導航欄屬於UINavigationController,該導航欄將視圖保存在其中,因此當您將其更改為一個時,它便會全部更改。

解決方案是在第二個視圖控制器的viewWillAppear方法中將其更改為半透明,而在viewDidDissapear方法中將其更改為不透明。 嘗試使用viewWill和viewDid顯示方法,直到找到適合您需要的方法為止。

試試這個,這會使您的導航欄透明。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM