为什么我的导航栏在 ViewController 上消失,然后当我向下滚动时又重新出现?

Mik*_*tti 1 xcode storyboard uinavigationcontroller ios swift

我不确定我的应用程序发生了什么变化。由于某种原因,最近当我尝试开发它时,我的应用程序上的导航栏开始消失,然后当我向下滚动时重新出现。这是演示这一点的屏幕截图。

是什么让我的导航栏消失了?

应用程序打开到左侧的屏幕截图,然后向下滚动以显示右侧的屏幕截图。

当向上滚动时当向下滚动时

这是一个全新的导航控制器,我在 Storyboard 上设置并在初始视图控制器中设置。新控制器的实际 swift 代码如下。

import UIKit

class NewsViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 50
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        let cell = UITableViewCell()

        // Configure the cell...
        cell.textLabel?.text = "Item \(indexPath.row)"

        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

我在应用程序委托中有以下代码

       UINavigationBar.appearance().tintColor = UIColor.primaryColor();
        UINavigationBar.appearance().barTintColor = UIColor.primaryColor();
        UINavigationBar.appearance().isOpaque = true;
        UINavigationBar.appearance().titleTextAttributes = convertToOptionalNSAttributedStringKeyDictionary([
            NSAttributedString.Key.foregroundColor.rawValue: UIColor.white
        ])
        
        UITabBar.appearance().barTintColor = UIColor.primaryColor();
        UITabBar.appearance().isOpaque = false;
        UITabBar.appearance().tintColor = UIColor.white;
        UIRefreshControl.appearance().tintColor = UIColor.white;
Run Code Online (Sandbox Code Playgroud)

Dmy*_*nko 6

那是因为在iOS15和Xcode13中你需要使用UINavigationBarAppearance来自定义导航栏。您需要更改 AppDelegate 中的代码,如下所示:

    let barAppearance = UINavigationBar.appearance()
    barAppearance.isTranslucent = false
    barAppearance.clipsToBounds = false
    
    let titleTextAttributes: [NSAttributedString.Key: Any] = [
        .foregroundColor: UIColor.white,
    ]
    
    if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundColor = UIColor.primaryColor()
        appearance.titleTextAttributes = titleTextAttributes

        barAppearance.standardAppearance = appearance
        barAppearance.scrollEdgeAppearance = appearance
    } else {
        barAppearance.setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.defaultPrompt)
        barAppearance.shadowImage = UIImage()
        barAppearance.barTintColor = UIColor.primaryColor()
        barAppearance.titleTextAttributes = titleTextAttributes
    }
    barAppearance.tintColor = .white
Run Code Online (Sandbox Code Playgroud)