UIDocumentPickerViewController导航栏按钮颜色

18

我的应用程序使用红色导航(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().barTintColor = .green - Anbu.Karthik
展示一些额外的代码。 - Anbu.Karthik
当我设置barTintColor时,它会改变根视图控制器的颜色。但在文档选择器中没有效果。 - Kryštof Matěj
我创建了一个示例项目 https://github.com/raptorxcz/UIDocumentPickerViewController-buttons-color - Kryštof Matěj
根据苹果公司的说法,这是一个漏洞。我已经向他们询问了解决方法。等我收到回复后,我会在这里添加它。 - Kryštof Matěj
显示剩余9条评论
4个回答

41

您可以通过在application:didFinishLaunchingWithOptions:函数中添加以下代码来仅重置UIDocumentPickerViewController的外观,这样工具栏按钮将返回到其原始蓝色或您可以设置任何其他颜色。然而,工具栏颜色是不可定制的。

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

这只适用于iOS 11.0及以上版本吗? - Ankit Kumar Gupta
1
这是我的解决方案,我将色调设置为白色,并且在UIDocumentBrowserViewController中看不到“取消”按钮。现在我已将其设置为蓝色。UINavigationBar.appearance().tintColor = UIColor.white if #available(iOS 11.0, *) { UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = UIColor.blue } - Im Batman
1
你好,我发现你对于UIDocumentBrowserViewController的回答很好用,但是我无法在UIImagePickerController上使用它,你有什么想法吗? - Paweł Zgoda-Ferchmin

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

请查看我在这里的回答


1

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

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

据我所知,无法仅设置条形颜色,只能设置色调颜色(文本颜色)。为了保留项目中其余部分的色调颜色,在基础视图控制器的viewWillAppear中重置它。

-2

当你询问“更改导航栏或文本的颜色时?”我没有更改导航栏的解决方案,它总是返回空值。

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

我知道这可能不是最优解,但我尝试的所有解决方案都不适用于我。


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