Skip to content

Repository files navigation

SwiftXcodeCocoaPods CompatibleMIT

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'dopod'NativeUI','~> 1.2.2'end

If you don't need to connect all UI components you may use subspecs like:

target'MyApp'dopod'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.
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)
  1. 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)
  1. Initialization with title, message and custom UIView object 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.

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.

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)

Author

Anton Poltoratskyi

License

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

About

iOS library that includes customizable replacements for native UIKit components

Topics

Resources

Stars

9 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages