Pun*_*any 23 navigation ios swift
我在导航控制器中嵌入了 2 个 ViewController,如下图所示。
每次我滚动浏览表格项目时,导航背景颜色都会随着状态栏背景颜色不断变化。
如何以编程方式设置导航栏和状态栏的背景颜色?
代码:
import UIKit
class TestViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var tableView: UITableView!
let faqList : [FAQ] = [
FAQ(title: "Test 1", content: "Answer 1"),
FAQ(title: "Test 2", content: "Answer 2"),
FAQ(title: "Test 3", content: "Answer 3"),
FAQ(title: "Test 4", content: "Answer 4"),
FAQ(title: "Test 5", content: "Answer 5"),
FAQ(title: "Test 6", content: "Answer 6"),
FAQ(title: "Test 7", content: "Answer 7"),
FAQ(title: "Test 8", content: "Answer 8"),
FAQ(title: "Test 9", content: "Answer 9"),
FAQ(title: "Test 10", content: "Answer 10"),
FAQ(title: "Test 11", content: "Answer 11"),
FAQ(title: "Test 12", content: "Answer 12"),
FAQ(title: "Test 13", content: "Answer 13"),
FAQ(title: "Test 14", content: "Answer 14"),
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = UIColor(named: "BackgroundColor")
tableView.register(UINib(nibName: "ButtonTableViewCell", bundle: nil), forCellReuseIdentifier: "ButtonCell")
self.navigationController?.navigationBar.backgroundColor = .blue
}
}
extension TestViewController: UITableViewDelegate {
}
extension TestViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return faqList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell", for: indexPath) as! ButtonTableViewCell
let buttonCell = faqList[indexPath.row]
cell.titleLabel.text = buttonCell.title
cell.trailingIconBackground.isHidden = true
cell.buttonView.backgroundColor = .brown
cell.buttonView.layer.cornerRadius = 10
cell.buttonView.layer.masksToBounds = true
cell.selectionStyle = UITableViewCell.SelectionStyle.none
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
Bik*_*kur 36
将其粘贴到 AppDelegate,如果您也有选项卡栏,则粘贴 tabbarappearance 也将起作用。
if #available(iOS 15, *) {
// MARK: Navigation bar appearance
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white
]
navigationBarAppearance.backgroundColor = UIColor.blue
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
// MARK: Tab bar appearance
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithOpaqueBackground()
tabBarAppearance.backgroundColor = UIColor.blue
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
UITabBar.appearance().standardAppearance = tabBarAppearance
}
Run Code Online (Sandbox Code Playgroud)
أبو*_*ill 11
对于您的导航栏,您会发现“外观”检查“标准”和“滚动边缘”然后您将在检查器中找到多个“背景”属性,将两个背景更改为“标准外观”部分下的一个和“滚动边缘”下的另一个边缘外观”部分,那么它应该可以正常工作
Rat*_*ath 11
只需简单地将空图像添加到导航栏背景,如下所示:
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
Run Code Online (Sandbox Code Playgroud)
只需使用下面的代码即可。我修复了同样的问题:
// Below code will fix Navigation bar issue fixed for iOS 15.0
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
self.navigationController?.navigationBar.isTranslucent = true // pass "true" for fixing iOS 15.0 black bg issue
self.navigationController?.navigationBar.tintColor = UIColor.white // We need to set tintcolor for iOS 15.0
appearance.shadowColor = .clear //removing navigationbar 1 px bottom border.
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
Run Code Online (Sandbox Code Playgroud)