- iOS 11.0
- Xcode 14.0
- Swift 5
dependencies: [
.package(url: "https://github.com/devpolant/NativeUI.git", .upToNextMajor(from: "1.2.2"))
]
target 'MyApp' do
pod 'NativeUI', '~> 1.2.2'
end
If you don't need to connect all UI components you may use subspecs like:
target 'MyApp' do
pod 'NativeUI/Alert', '~> 1.2.2'
end
Available subspecs:
Utils
Alert
AlertViewController
is a customizable replacement for native UIAlertController
.
Sometimes we need to set NSAttributedString into native alert, but public API doesn't allow it. As a workaroud we could use private API, but in general we should avoid using it.
AlertViewController
looks exactly like native UIAlertController
, but very configurable.
- Default initialization with title, message as
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)
- Default initialization with title, message as
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)
- Initialization with title, message and custom
UIView
object as content view to implement complex layout.
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)
See Alert.swift for more details.
When you need to present few alerts in a row you should set shouldDismissAutomatically
flag to false
and add action AFTER view controller initialization to be able to capture alert instance in action handler's closure.
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 is available under the MIT license. See the LICENSE file for more info.