如何定制UIDocumentPickerViewController及部分视图控制器的导航栏样式?
解决iOS全局导航栏样式与特定视图控制器反转样式的问题
嘿,这个场景我之前开发时也碰到过——全局统一了导航栏样式后,系统自带的像UIDocumentPickerViewController这类VC或者部分自定义VC需要反转样式,确实有点头疼。不过有几个靠谱的方案可以解决,咱们一个个来看:
1. 用UIAppearance的容器类设置(最推荐)
iOS 9及以后,UIAppearance提供了appearance(whenContainedInInstancesOf:)方法,可以针对特定容器类里的导航栏单独设置样式,完美适配你的需求。
你只需要在AppDelegate的didFinishLaunchingWithOptions里,在全局设置之后,再添加针对目标VC的反转样式配置就行:
// 先保留你原来的全局导航栏设置 UINavigationBar.appearance().isTranslucent = false UINavigationBar.appearance().tintColor = .white UINavigationBar.appearance().barTintColor = AppColors.blue UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] // 针对UIDocumentPickerViewController设置反转样式 UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentPickerViewController.self]).barTintColor = .white UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentPickerViewController.self]).tintColor = AppColors.blue UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentPickerViewController.self]).titleTextAttributes = [NSAttributedString.Key.foregroundColor: AppColors.blue]
这种方式的好处是一劳永逸,所有属于这个类的VC都会自动应用反转样式,不用每个VC单独写代码,也不用担心样式恢复的问题。
2. 自定义VC在生命周期中手动修改/恢复样式
如果是你自己写的自定义VC需要反转样式,可以在VC的viewWillAppear和viewWillDisappear方法里手动切换样式,记得在页面消失时恢复全局样式,避免影响后续页面:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) guard let navBar = navigationController?.navigationBar else { return } // 应用反转样式 navBar.barTintColor = .white navBar.tintColor = AppColors.blue navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: AppColors.blue] } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) guard let navBar = navigationController?.navigationBar else { return } // 恢复全局样式 navBar.barTintColor = AppColors.blue navBar.tintColor = .white navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] }
这个方法适合自定义VC的场景,灵活性高,但要注意如果VC是被present而不是push的,也要确保dismiss时恢复样式。
3. 临时修改全局样式(针对特殊系统VC的备选方案)
如果某些系统VC不支持第一种容器类设置(虽然大部分都支持),可以在present该VC前临时修改全局导航栏样式,然后在dismiss时恢复。不过这个方法要注意必须严格执行恢复操作,不然会影响其他页面:
// 先保存当前的全局样式 let originalBarTint = UINavigationBar.appearance().barTintColor let originalTint = UINavigationBar.appearance().tintColor let originalTitleAttrs = UINavigationBar.appearance().titleTextAttributes // 临时设置反转样式 UINavigationBar.appearance().barTintColor = .white UINavigationBar.appearance().tintColor = AppColors.blue UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: AppColors.blue] // 初始化并present文档选择器 let picker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import) picker.delegate = self present(picker, animated: true) // 在代理方法中恢复样式 func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { controller.dismiss(animated: true) { self.restoreNavigationBarStyle(originalBarTint: originalBarTint, originalTint: originalTint, originalTitleAttrs: originalTitleAttrs) } } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { // 处理选中的文件逻辑... controller.dismiss(animated: true) { self.restoreNavigationBarStyle(originalBarTint: originalBarTint, originalTint: originalTint, originalTitleAttrs: originalTitleAttrs) } } // 封装恢复样式的方法 private func restoreNavigationBarStyle(originalBarTint: UIColor?, originalTint: UIColor?, originalTitleAttrs: [NSAttributedString.Key : Any]?) { UINavigationBar.appearance().barTintColor = originalBarTint UINavigationBar.appearance().tintColor = originalTint UINavigationBar.appearance().titleTextAttributes = originalTitleAttrs }
这个方法作为备选,尽量优先用第一种方案,因为临时修改全局样式容易出现遗漏恢复的情况,导致其他页面样式错乱。
内容的提问来源于stack exchange,提问作者Isuru
相关产品推荐
相关产品推荐

