【问题标题】:navigationBar .tintColor with UIDocumentPickerViewController (Document Picker)?带有 UIDocumentPickerViewController(文档选择器)的 navigationBar .tintColor?
【发布时间】:2018-03-14 17:34:04
【问题描述】:

我在 Xcode 7.0 beta 6 中使用 Swift 2

长话短说,我正在研究如何在使用文档选择器访问 iCloud 时设置 .navigationBar.barStylenavigationBar.tintColor - 即 UIDocumentPickerViewController

我已经尝试过,例如:

/...
documentPicker.navigationController!.navigationBar.barStyle = UIBarStyle.Default
documentPicker.navigationController!.navigationBar.tintColor = UIColor.orangeColor()
/...

例如。在这里,我在导航控制器中嵌入了一个视图控制器:

MyNavigationController我可以设置.barStyle.tintStyle如下:

class MyNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.barStyle = UIBarStyle.Default
        self.navigationBar.tintColor = UIColor.orangeColor()
    }
}

所以.tintStyle是橙色的如下:

iCloud 已启用并且FirstViewController 符合UIDocumentPickerDelegate。条形按钮调用IBAction 函数,如FirstViewController 的代码所示:

class FirstViewController: UIViewController, UIDocumentPickerDelegate {
    // ...
    @IBAction func importDocument(sender: UIBarButtonItem) {
        let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.text"], inMode: UIDocumentPickerMode.Import)
        documentPicker.delegate = self
        documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
        documentPicker.popoverPresentationController?.barButtonItem = sender
        self.presentViewController(documentPicker, animated: true, completion: nil)
}

    func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
        // ...
    }

    func documentPickerWasCancelled(controller: UIDocumentPickerViewController) {
        // ...
    }
}

这行得通。文档选择器按预期加载:

但是。为了弄清楚如何做到这一点,我希望菜单项“完成”为橙色。和以前一样。

我已尝试将以下代码添加到@IBAction,如下所示:

//...
documentPicker.navigationController!.navigationBar.barStyle = UIBarStyle.Default
documentPicker.navigationController!.navigationBar.tintColor = UIColor.orangeColor()
self.presentViewController(documentPicker, animated: true, completion: nil)
//...

这不起作用,因为此时 documentPicker.navigationControllernil

谁能告诉我在周期内如何或在哪里可以访问documentPicker.navigationController!.navigationBar.tintColor

或者也许我遗漏了什么,还有其他方法可以改变菜单颜色?

或者也许我应该创建一个自定义导航控制器 - 和一个自定义文档选择器视图控制器。然后理论上我可以访问相关的viewDidLoad。我试过了,但意识到我还需要一个自定义版本的UIDocumentPickerDelegate 协议。肯定有一个更简单的解决方案(我怀疑这是否会被允许)。

【问题讨论】:

    标签: ios swift uidocumentpickervc


    【解决方案1】:

    您可以通过UIAppearance 更改导航栏的默认色调颜色:

    UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    

    【讨论】:

    • 太好了。谢谢。
    • 这将改变整个应用程序的色调颜色,我们可以只改变 documentViewController 的色调颜色
    • 同意@YogeshPatel 的评论。
    • [UINavigationBar 外观].tintColor = [UIColor yourColor]; // 按钮标题颜色和 [UINavigationBar 外观].backgroundColor = [UIColor yourColor]; // 用于背景颜色。像魅力一样为我工作。我只有 iOS 14 版本有这个问题。
    【解决方案2】:

    swift 3,4 和 xcode 9+

    documentPicker.view.tintColor = .orange
    

    【讨论】:

    • 它改变视图项的颜色不是条形按钮的颜色。
    • 我知道,但是在文档选择器中没有导航栏,项目是视图的子视图,因此可以解决问题
    • 不,它不工作。查看色调颜色将应用于它的子视图。以上答案工作正常。
    • 我的代码不适合我的答案,可能是IOS版本不同,如果你找到了答案为什么告诉我
    • 如果我发现了什么,我会告诉你的。
    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2018-05-05
    • 1970-01-01
    • 2021-09-25
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多