NativeUI 1.2.2

NativeUI 1.2.2

Maintained by Anton Poltoratskyi.



NativeUI 1.2.2

  • By
  • Anton Poltoratskyi

Swift Xcode CocoaPods Compatible MIT

NativeUI

Minimum Requirements:

  • iOS 11.0
  • Xcode 14.0
  • Swift 5

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/devpolant/NativeUI.git", .upToNextMajor(from: "1.2.2"))
]

CocoaPods

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

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.

Configuration

  1. 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)
  1. 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)
  1. 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.

Edge cases

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)

Author

Anton Poltoratskyi

License

NativeUI is available under the MIT license. See the LICENSE file for more info.