为什么UISearchController更改导航栏颜色? [英] Why is UISearchController changing the navigation bar colors?

查看:260
本文介绍了为什么UISearchController更改导航栏颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在一个示例项目中对此进行了测试,该示例项目使用Xcode 11(iOS 13)在情节提要中定义了2个视图控制器. 呈现"视图控制器嵌入在导航控制器中,并在viewWillAppear中设置了导航栏颜色. 搜索"视图控制器在viewDidLoad中添加UISearchController,并由当前视图控制器推动(非模态).

I have tested this on a sample project with 2 view controllers defined in the storyboard using Xcode 11 (iOS 13). The "presenting" view controller is embedded in a navigation controller and has the navigation bar colors set in the viewWillAppear. The "search" view controller adds a UISearchController in the viewDidLoad and is pushed by the presenting view controller (NOT modal).

仅通过这种设置,当显示搜索视图控制器时,导航栏将具有预期的蓝色背景和红色.但是,当向下滚动并显示搜索栏时,导航栏的背景色会丢失(或更改为默认的iOS灰色/半透明外观).但是,如果您向上滚动(隐藏搜索栏)或专注于搜索栏文本字段,则导航栏的颜色会返回!

With just this setup when the search view controller is displayed the navigation bar has the blue background and red tint color as expected. However when scrolling down and the search bar is displayed the background color of the navigation bar is lost (or changed to what appears to be the default iOS grey / translucent). However if you scroll back up (hide the search bar) or focus on the search bar text field the navigation bar color returns!

此外,如果您专注于搜索栏文本字段,然后取消(通过点击取消"按钮),导航栏的色调颜色将从红色恢复为默认的iOS蓝色,如后栏项所示.

Also if you focus on the search bar text field and then cancel (by tapping the Cancel button) the tint color of the navigation bar reverts from red to the default iOS blue as can be noticed by the back bar item.

关于解决此问题的任何建议吗?

Any suggestions on resolving this issue?

我也在搜索控制器的viewWillAppear中设置了导航栏颜色,但这并没有改变这种行为.

I have set the navigation bar colors in the viewWillAppear of the search controller too which didn't change this behaviour.

我在搜索控制器中将导航栏的isTranslucent设置为true,这似乎阻止了背景颜色的还原,但是在取消时它并没有改变色调的还原.

I set isTranslucent to true for the navigation bar in the search controller which seemed to prevent the reverting of the background color but it did not change the reverting of the tint color on cancel.

呈现视图控制器

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

    navigationController?.navigationBar.barTintColor = .blue
    navigationController?.navigationBar.tintColor = .red
}

Search View Controller

Search View Controller

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Search VC"

    searchController.dimsBackgroundDuringPresentation = false
    searchController.obscuresBackgroundDuringPresentation = false

    navigationItem.searchController = searchController

    definesPresentationContext = true
}

编辑

按照建议的方式设置scrollEdgeAppearancebackButtonAppearancebuttonAppearance可以有效,但是系统栏按钮默认为iOS蓝色.可以通过设置UINavigationBar.tintColor来解决此问题,但都不能解决取消搜索后默认返回的后退按钮V形颜色.

Setting the scrollEdgeAppearance, backButtonAppearance and buttonAppearance as suggested works a treat except for system bar buttons that default to the iOS blue. This can be resolved by setting the UINavigationBar.tintColor but neither that resolves the back button chevron color defaulting on cancel of the search.

if #available(iOS 13.0, *) {
    let buttonAppearance = UIBarButtonItemAppearance()
    buttonAppearance.configureWithDefault(for: .plain)
    buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.red]

    let navigationBarAppearance = UINavigationBarAppearance()
    navigationBarAppearance.configureWithOpaqueBackground()
    navigationBarAppearance.backgroundColor = .blue
    navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.red]

    navigationBarAppearance.backButtonAppearance = buttonAppearance
    navigationBarAppearance.buttonAppearance = buttonAppearance
    navigationBarAppearance.doneButtonAppearance = buttonAppearance

    navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
    navigationController?.navigationBar.compactAppearance = navigationBarAppearance
    navigationController?.navigationBar.standardAppearance = navigationBarAppearance
} else {
    navigationController?.navigationBar.barTintColor = .blue
    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.red]
    navigationController?.navigationBar.tintColor = .red
}

推荐答案

但是,当向下滚动并显示搜索栏时,导航栏的背景颜色会丢失

However when scrolling down and the search bar is displayed the background color of the navigation bar is lost

所有这些都是正常的. iOS 13中的新增功能,扩展后的导航栏(显示搜索栏,大标题等)与普通导航栏的外观不同.您的设置仅适用于普通导航栏,因为您未将它们设置为iOS 13方式.如果要使扩展的导航栏看起来像普通导航栏,则必须分别并明确地设置其外观.

All of that is normal. New in iOS 13, the expanded nav bar (displaying search bar, large title, etc.) has different appearance from the normal nav bar. Your settings applied only to the normal nav bar because you didn't make them the iOS 13 way. If you want the expanded nav bar to look like the normal nav bar, you have to set its appearance separately and explicitly.

为此,您需要设置导航栏的scrollEdgeAppearance.研究类UIBarAppearance,UINavigationBarAppearance和UIBarButtonItemAppearance(您需要显式设置backButtonAppearance).

To do so, you need to set the navigation bar's scrollEdgeAppearance. Investigate classes UIBarAppearance, UINavigationBarAppearance, and UIBarButtonItemAppearance (you will need to set the backButtonAppearance explicitly).

这篇关于为什么UISearchController更改导航栏颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆