NavigationLayers
入门
我们构建应用程序的方式正在改变;UIViewController正在变得更加简洁,应该只处理用户交互和布局。我经常发现,当构建一个服务时,我可能需要在应用程序中的任何位置显示一个警告,那么为什么不在一个新窗口中加载它而不是试图找到顶级的UIViewController呢?
在您的应用程序中有很多视图堆叠吗?为什么不将一些屏幕流程呈现在新窗口中呢。
NavigationLayers 为您提供了在新 UIWindow(我称之为 NavigationLayer)中呈现任何 UIViewController 的功能。最好的部分是,一旦您关闭第一个加载到 NavigationLayer 的 UIViewController,新 UIWindow 及其根视图就会被从内存中清除。
让我们看看如何在一个新的 NavigationLayer(UIWindow)中显示标准的 UIAlertViewController 的示例。
这里我们使用默认的 create 函数,它使用了默认选项,做了三件事。首先是允许一次只有一个新的 NavigationLayer,任何添加的新 NavigationLayer 都不会显示,因为你不想最终堆积无数个 alert 观视图。其次和第三项选项用于 isStatusBarHidden 和 UIStatusBarStyle,这些选项用于新 NavigationLayer,它们采用当 NavigationLayer 呈现新 Layer 时可见的 UIViewController 正在使用的当前设置。
// Here we create the alert
let alert = UIAlertController(title: "New Window", message: "Alert in a new window or NavigationLayer", preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default) { (_) in
alert.dismiss(animated: true, completion: nil)
}
alert.addAction(action)
// Here we use the default function create that uses the default NavigationLayerOptions()
NavigationLayer.create(with: alert, animated: false, completion: nil)
一旦您关闭 UIAlertViewViewController,系统就会从内存中删除 NavigationLayer。
进阶功能
如果您想拥有更多控制权,可以使用“使用选项创建”功能,该功能可配置为允许多NavigationLayer堆叠,并更改新NavigationLayer的UIStatusBarStytle和isStatusBarHidden选项。
// Here we create the alert
let alert = UIAlertController(title: "New Window", message: "Alert in a new window or NavigationLayer", preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default) { (_) in
alert.dismiss(animated: true, completion: nil)
}
// Here we use the create function with options that uses a custom NavigationLayerOptions()
let options = NavigationLayerOptions(allowWindowStacking: true)
NavigationLayer.create(with: options, and: alertOne, animated: false, completion: nil)
NavigationLayerOptions中的每个参数都有一个默认值,因此您只需编辑感兴趣的价值而非全部三者。
NavigationLayerOptions(allowWindowStacking: <#T##Bool#>, prefersStatusBarHidden: <#T##Bool#>, statusBarStyle: <#T##UIStatusBarStyle#>)
示例
要运行示例项目,请先克隆仓库,然后从Example目录中运行pod install
。
需求
iOS 11.00及以上
安装
NavigationLayers可通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中:
pod 'NavigationLayers'
作者
Alexander Karan, [email protected]
许可协议
NavigationLayers在MIT许可下可用。有关更多信息,请参阅LICENSE文件。