UIDocumentPickerViewController 导航栏按钮颜色

UIDocumentPickerViewController navigation bar buttons color

我的应用使用带有白色按钮和文本的红色导航 (2) 栏。当我使用系统联系人选择器 (3) 时,状态栏为红色。当我使用文档选择器 (1) UIDocumentPickerViewController 时,导航栏是白色的。如何更改导航栏或文本的颜色?

当我使用下面的代码时,它有效但它也改变了我的导航栏。

UINavigationBar.appearance().tintColor = .red

感谢帮助

代码:

func open() {
        UINavigationBar.appearance().barTintColor = .green
        let documentsController = UIDocumentPickerViewController(documentTypes: makeDocumentTypesList(), in: .import)
        documentsController.delegate = self
        viewControllerProvider.getViewController().present(documentsController, animated: true, completion: nil)
    }

这是我找到的唯一解决方案:

    UINavigationBar.appearance().tintColor = ... some color
    _viewController?.present(documentPicker, animated: true)

据我所知,无法设置 bar color,只能设置 tint color(文本颜色)。为了保持项目其余部分的色调,我在底层视图控制器的 viewWillAppear 中重置了它。

使用setTitleTextAttributes

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: .normal)

see my answer here

如你所愿"change color of navigation bar or text?" 我没有更改导航栏的解决方案,它总是 return nil

self.present(documentPicker, animated: true,completion: {

                if documentPicker.navigationController?.navigationBar != nil{
                    documentPicker.navigationController?.navigationBar.barTintColor = .red
                }
            })

但如果您同意仅更改文本

这对我有用

self.present(documentPicker, animated: true,completion: {

                documentPicker.view.tintColor = .red
            })

我知道它可能不是最优的,但是 none 我尝试的解决方案对我有用

您只需将此代码放在 application:didFinishLaunchingWithOptions: 函数中的某处,即可重置 UIDocumentPickerViewController 的外观,条形按钮将 return 恢复为原来的蓝色,或者您可以设置任何您选择的其他颜色。另一方面,条形颜色不可自定义。

if #available(iOS 11.0, *) {
    UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil
}