【问题标题】:Why is my navigation bar disappearing on my ViewController and then re-appearing when I scroll down?为什么我的导航栏在我的 ViewController 上消失了,然后当我向下滚动时又重新出现?
【发布时间】:2022-01-23 00:31:16
【问题描述】:

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

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

应用打开到左侧的屏幕截图并向下滚动以显示右侧的屏幕截图。

这是一个全新的导航控制器,我在 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
    }
}

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

       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;

【问题讨论】:

    标签: ios swift xcode uinavigationcontroller storyboard


    【解决方案1】:

    那是因为在 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
    

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 1970-01-01
      • 2020-08-02
      • 2012-06-23
      • 2021-02-22
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2018-08-14
      相关资源
      最近更新 更多