- iOS 11.0
- Xcode 14.0
- Swift 5
dependencies:[.package(url:"https://github.com/devpolant/NativeUI.git",.upToNextMajor(from:"1.2.2"))]target'MyApp'dopod'NativeUI','~> 1.2.2'endIf you don't need to connect all UI components you may use subspecs like:
target'MyApp'dopod'NativeUI/Alert','~> 1.2.2'endAvailable subspecs:
UtilsAlert
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.
letcancelAction=Alert.Action(title:"Cancel", style:.primary)letconfirmAction=Alert.Action(title:"Confirm", style:.default)letviewModel=Alert(
title:"Your Title",
titleFont:... // your custom title font
message:"Your Message",
messageFont:... // your custom message font
actions:[cancelAction, confirmAction])letalert=AlertViewController(viewModel: viewModel)present(alert, animated:true)- Default initialization with title, message as
NSAttributedString
letcancelAction=Alert.Action(title:"Cancel", style:.primary)letconfirmAction=Alert.Action(title:"Confirm", style:.default)letviewModel=Alert(
title:... // your title (NSAttributedString)
message:... // your message (NSAttributedString)
actions:[cancelAction, confirmAction])letalert=AlertViewController(viewModel: viewModel)present(alert, animated:true)- Initialization with title, message and custom
UIViewobject as content view to implement complex layout.
letcancelAction=Alert.Action(title:"Cancel", style:.primary)letconfirmAction=Alert.Action(title:"Confirm", style:.default)letcustomView=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."letviewModel=Alert(
title:"Your Title",
message:nil,
contentView: customView,
actions:[cancelAction, confirmAction])letalert=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.
letviewModel=Alert(
title:"Your Title",
message:"Your Message")letalert=AlertViewController(viewModel: viewModel)
alert.shouldDismissAutomatically =falseletcancelAction=Alert.Action(title:"Cancel", style:.primary){[weak alert] _ in
alert?.dismiss(animated:true){
// present something else
}}
alert.addAction(cancelAction)letconfirmAction=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.

