首页 > 解决方案 > 带有UITabBarController的searchBarController的导航栏在更改导航栏的颜色时闪烁

问题描述

当我在 iOS (iPhone X) 中滚动时,导航栏的底线(具有搜索控制器)闪烁。尝试了许多解决方案,但都没有奏效。这是我的代码:

(我没有使用故事板)

应用委托

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow()
    let viewCtrl = TabController()
    let navCtrl = UINavigationController(rootViewController: viewCtrl)
    self.window?.rootViewController = navCtrl
    self.window?.makeKeyAndVisible()
    return true
}

标签栏控制器

class TabController: UITabBarController {

    init() {
        super.init(nibName: nil, bundle: nil)
        self.viewControllers = [ViewController()]
        self.navigationItem.searchController = UISearchController(searchResultsController: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.barTintColor = .white
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    }
}

视图控制器

class ViewController: UITableViewController {

    init() {
        super.init(nibName: nil, bundle: nil)
        self.view.backgroundColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "Test"
        return cell
    }

}

帮助将不胜感激。

标签: iosswiftuitabbarcontrolleruinavigationbar

解决方案


我发现了同样的问题,我认为是 Apple 的错误。但无论如何,如果有帮助,制作导航栏isTranslucent = false可以帮助避免这种丑陋的行为。


推荐阅读