Swift 和导航:滚动视图时导航栏会更改其背景颜色

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

对于您的导航栏,您会发现“外观”检查“标准”和“滚动边缘”然后您将在检查器中找到多个“背景”属性,将两个背景更改为“标准外观”部分下的一个和“滚动边缘”下的另一个边缘外观”部分,那么它应该可以正常工作

  • 这是 100% 正确的答案。几天来一直试图解决这个问题。在 15.4 (xcode 13.3) 中为我修复了它 (3认同)
  • 我会在这里给你一些爱......这对我有用......它让我惊讶你必须“恰到好处”地实现你想要的效果的所有选项和杠杆。例如,不透明和半透明有什么区别?如果你只选了 1 个怎么办?都选了?都没选?这样的难题......再次感谢。 (2认同)
  • 嘿,谢谢大家,很高兴看到我的评论实际上有帮助,哈哈 (2认同)

Rat*_*ath 11

只需简单地将空图像添加到导航栏背景,如下所示:

 navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
 navigationController?.navigationBar.shadowImage = UIImage()
Run Code Online (Sandbox Code Playgroud)


Kud*_*dos 9

只需使用下面的代码即可。我修复了同样的问题:

// 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)