为什么我的导航栏在我的 ViewController 上消失,然后在我向下滚动时重新出现?
Why is my navigation bar disappearing on my ViewController and then re-appearing when I scroll down?
我不确定我的应用程序发生了什么变化。出于某种原因,最近当我尝试开发它时,我的应用程序上的导航栏开始消失,然后在我向下滚动时重新出现。这是演示此操作的屏幕截图。
是什么让我的导航栏消失了?
应用程序打开到左侧的屏幕截图并向下滚动以显示右侧的屏幕截图。
这是我在 Storyboard 上设置并设置在初始视图控制器上的全新 Navigation Controller。新控制器的实际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;
那是因为在 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
我不确定我的应用程序发生了什么变化。出于某种原因,最近当我尝试开发它时,我的应用程序上的导航栏开始消失,然后在我向下滚动时重新出现。这是演示此操作的屏幕截图。
是什么让我的导航栏消失了?
应用程序打开到左侧的屏幕截图并向下滚动以显示右侧的屏幕截图。
这是我在 Storyboard 上设置并设置在初始视图控制器上的全新 Navigation Controller。新控制器的实际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;
那是因为在 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