当向导栏添加UISearchController时,导航栏变为白色。

9
在我的 UINavigationController 中添加 UISearchController 后,当视图加载时,它会变为白色,并在用户单击搜索栏时更改为指定的颜色。这种情况自 iOS 13.1 以来发生。以下视频显示了这种行为:https://imgur.com/wn5zbnJ 我的代码由一个简单的故事板组成,其中包含 NavigationController + TableViewController,NavigationController 具有分配的颜色:enter image description here ViewController 包含以下代码:
class ViewController: UITableViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = false
        navigationItem.searchController = searchController
    }
}

我还将这些键添加到info.plist文件中,以强制应用程序进入浅色模式,但如果我删除它们,相同的行为仍然存在:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

这是在运行iOS 13.1 beta 1的iPhone XS Max上测试的。这是否是预期行为或需要修复的错误?

你想让导航栏一直保持红色吗?你想在导航栏中添加搜索栏吗? - Yunus Karakaya
@YunusKarakaya 是的,我希望导航栏始终为红色,并且在导航栏中也有搜索栏。 - Devxln
很遗憾,我在iOS 13.1上也有同样的问题。 - iDevid
@Devxln 你修好了吗?我也遇到了同样的问题。 - kai
4个回答

22

看起来需要在iOS 13上使用新的UINavigationBarAppearance。尝试将以下内容添加到您的viewDidLoad中:

let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemRed
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

你可能还想设置搜索字段的背景颜色:

let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground

6
这似乎是iOS 13.1中的一个错误。具体来说,导航栏有一个名为UINavigationBarAppearance的新外观,它指定了滚动视图滚动到顶部时的外观,以及默认状态下的外观。通常,只有使用相应SDK(iOS 13.1)构建应用程序时,此类更改才会生效。然而,似乎存在这样的一个错误,即当使用iOS 12 SDK构建应用程序时,也会发生此类行为。
参见:https://developer.apple.com/documentation/uikit/uinavigationbarappearance 更新: 这里有一个解决方法:https://itnext.io/fixing-issues-caused-by-future-sdks-ae0896384abf 实质上,如果您的应用程序在运行iOS 13的设备上运行,则可以在swift中通过NSClassFromString()创建新类的实例,然后使用一些objective-c运行时魔法来配置导航栏。

好的,没有临时解决办法吗? - Devxln
不,我们不能让UIKit根据使用的SDK而表现不同。一种解决方法是更改导航栏的内部背景视图,但这取决于导航栏视图层次结构的特定结构,这是一个实现细节,随时可能会更改。 - Scott Ahten
2
开发人员报告称,这个问题在iOS 13.2中已经解决。 - Scott Ahten

3

您的问题并不是很清楚。如果您想要将搜索栏添加到导航栏中,并使用特定颜色,这可能会对您有所帮助。

将搜索栏放入导航栏的过程:

let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController

您可以将任何控制器添加到“searchResultsController”值中。

如果您想将背景颜色设置为特定颜色,您可以从Storyboard -> navigationbar -> navigation bar attiribute inspection更改栏的色调。

此外,AppDelegate.swift文件中的以下代码将执行相同的操作。“tintcolor”和“titletextcolor”已被注释。

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.barTintColor = UIColor.blue

        // navigationBarAppearace.tintColor = UIColor.red
        //navigationBarAppearace.titleTextAttributes = [.foregroundColor: UIColor.red]
        return true
    }

抱歉,但这也不是我的问题的答案;它与iOS 13有关。自从iOS 13以来,我描述的行为就发生了。即使我在AppDelegate中添加那些全局颜色值。 - Devxln
我在帖子中添加了这个问题发生自iOS 13.1的信息。 - Devxln
@Devxln 我下载了 beta 13.1,但我没有遇到任何意外的行为。 - Yunus Karakaya

0

我遇到了同样的问题,并已向苹果开发者提交了一份报告。 即使将backgroundColor设置为UINavigationBar的外观,当下拉并显示搜索栏时,导航栏不会变成透明,而是变成该颜色,但状态栏仍然保持白色。

UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
UINavigationBar.appearance().backgroundColor = .red

这里有一些代码尝试减轻这种行为,但我愿意听听状态栏的事情


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接