我不確定我的應用程式發生了什么變化。最近出于某種原因,當我嘗試開發它時,我的應用程式上的導航欄開始消失,然后在我向下滾動時重新出現。這是演示這一點的螢屏截圖。
是什么讓我的導航欄消失?
應用程式打開左側的螢屏截圖并向下滾動以顯示右側的螢屏截圖。


這是一個全新的導航控制器,我在 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;
uj5u.com熱心網友回復:
那是因為在 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/390832.html
