我有一个用 swift 编写的 iOS 应用程序,它覆盖了 AppDelegate
中的默认导航栏背景:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navBackgroundImage = UIImage(named: "navBck")
UINavigationBar.appearance().barTintColor = UIColor(patternImage: navBackgroundImage)
}
在应用程序中,我想使用 UIDocumentInteractionController
来显示应用程序生成的 PDF 文件。
let docInteractionController = UIDocumentInteractionController(url: pdfURL!)
docInteractionController.delegate = self
docInteractionController.presentPreview(animated: true)
问题是我的自定义背景似乎不受支持,并且崩溃并出现以下错误(如果没有 AppDelegate
中的自定义背景,则不会发生这种情况)
-[UICGColorencodeWithCoder:]、/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.7.47/UIColor.m:1549 中断言失败 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“在这种情况下仅支持 RGBA 或白色颜色空间。”
我正在寻找最干净、最简单的方法来解决这个问题,为此操作将 ARGB 背景恢复到导航栏。我在 UIDocumentInteractionControllerDelegate
实现中尝试了以下方法,但没有成功(因相同的错误而崩溃)
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
UINavigationBar.appearance().barTintColor = UIColor.darkGrey
return self
}
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController)
{
let navBackgroundImage:UIImage! = UIImage(named: "navBck")
UINavigationBar.appearance().barTintColor = UIColor(patternImage: navBackgroundImage)
}
最佳答案
UIDocumentInteractionControllerDelegate 文档摘录:
// If preview is supported, this provides the view controller on which the preview will be presented.
// This method is required if preview is supported.
// If presenting atop a navigation stack, provide the navigation controller in order to animate in a manner consistent with the rest of the platform.
@available(iOS 3.2, *)
optional public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
此外,在我的新项目中,我总是使用 navigationController?.navigationBar.barTintColor
而不是 UINavigationBar.appearance().barTintColor
。
编辑:新示例,对 UIDocumentInteractionController 有更多了解
呈现 UIDocumentInteractionController 的 ViewController
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationController?.navigationBar.barTintColor = getCustomTintColor
}
func getCustomTintColor() -> UIColor{
let navBackgroundImage:UIImage! = UIImage(named: "navBck")
return UIColor(patternImage: navBackgroundImage)
}
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
navigationController?.navigationBar.barTintColor = UIColor. darkGrey
return navigationController!
}
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
navigationController?.navigationBar.barTintColor = getCustomTintColor
}
说实话,我也觉得如果你的话根本就不会发生崩溃
- 使用
navigationController?.navigationBar.barTintColor
而不是UINavigationBar.appearance().barTintColor
- 在
documentInteractionControllerViewControllerForPreview()
中返回navigationController
这样,您的预览屏幕也将具有正确的色调。 请提供图像,以便我可以检查我的示例项目(或者在您的代码中自行检查:))。
关于ios - UIDocumentInteractionController 的 UINavigationBar 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43718640/