NativeUI
最低要求
- iOS 11.0
- Xcode 14.0
- Swift 5
安装
Swift 包管理器
dependencies: [
.package(url: "https://github.com/devpolant/NativeUI.git", .upToNextMajor(from: "1.2.2"))
]
CocoaPods
target 'MyApp' do
pod 'NativeUI', '~> 1.2.2'
end
如果您不需要连接所有 UI 组件,您可以使用子规范,例如
target 'MyApp' do
pod 'NativeUI/Alert', '~> 1.2.2'
end
可用子规范
Utils
Alert
Alert
AlertViewController
是原生UIAlertController
的可定制替代品。
有时我们需要将NSAttributedString设置到原生警报中,但公共API不允许这样做。作为一个解决方案,我们可以使用私有API,但通常我们应该避免使用它。
AlertViewController
看起来与原生UIAlertController
完全一样,但非常可配置。
配置
- 使用标题和消息作为
String
进行默认初始化。
let cancelAction = Alert.Action(title: "Cancel", style: .primary)
let confirmAction = Alert.Action(title: "Confirm", style: .default)
let viewModel = Alert(
title: "Your Title",
titleFont: ... // your custom title font
message: "Your Message",
messageFont: ... // your custom message font
actions: [cancelAction, confirmAction]
)
let alert = AlertViewController(viewModel: viewModel)
present(alert, animated: true)
- 使用标题和消息作为
NSAttributedString
进行默认初始化。
let cancelAction = Alert.Action(title: "Cancel", style: .primary)
let confirmAction = Alert.Action(title: "Confirm", style: .default)
let viewModel = Alert(
title: ... // your title (NSAttributedString)
message: ... // your message (NSAttributedString)
actions: [cancelAction, confirmAction]
)
let alert = AlertViewController(viewModel: viewModel)
present(alert, animated: true)
- 使用标题、消息和自定义
UIView
对象作为内容视图进行初始化,以实现复杂的布局。
let cancelAction = Alert.Action(title: "Cancel", style: .primary)
let confirmAction = Alert.Action(title: "Confirm", style: .default)
let customView = CustomView()
customView.translatesAutoresizingMaskIntoConstraints = false
customView.imageView.backgroundColor = .orange
customView.titleLabel.text = "Some text"
customView.subtitleLabel.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
let viewModel = Alert(
title: "Your Title",
message: nil,
contentView: customView,
actions: [cancelAction, confirmAction]
)
let alert = AlertViewController(viewModel: viewModel)
present(alert, animated: true)
有关更多信息,请参阅Alert.swift。
边缘情况
当您需要连续显示几个警报时,应将shouldDismissAutomatically
标志设置为false
,并在视图控制器初始化后添加操作,以便能够在操作处理器的闭包中捕获警报实例。
let viewModel = Alert(
title: "Your Title",
message: "Your Message"
)
let alert = AlertViewController(viewModel: viewModel)
alert.shouldDismissAutomatically = false
let cancelAction = Alert.Action(title: "Cancel", style: .primary) { [weak alert] _ in
alert?.dismiss(animated: true) {
// present something else
}
}
alert.addAction(cancelAction)
let confirmAction = Alert.Action(title: "Confirm", style: .default) { [weak alert] _ in
alert?.dismiss(animated: true) {
// present something else
}
}
alert.addAction(confirmAction)
present(alert, animated: true)
作者
Anton Poltoratskyi
许可证
NativeUI 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。