Skip to main content

Custom Layouts

Brand Diyalog SDK dialogs with your own view controllers. Your app controls the look and feel; the SDK keeps driving the behavior, such as running the alert actions.

The iOS SDK currently supports this customization surface:

  • Alert Dialog: present SDK alerts with your own custom view controller and layout.

For colors, backgrounds and typography, see Style your SDK.

Overview#

The Diyalog iOS SDK lets your app supply its own UI for critical SDK views. For alert dialogs, you conform to the DiyalogCustomAlertDelegate protocol. Once the delegate is registered, the SDK hands every alert presentation to your app, passing along the title, the message and the action models to show.

Design principles#

  • Opt-in: if you never register the custom delegate, the SDK uses the default system UIAlertController.
  • Contract by delegate: your app implements the presentation and must make sure the handler of each provided action is invoked when the user taps its button.
  • Register early: assign the delegate in AppDelegate's didFinishLaunchingWithOptions, next to the rest of your Diyalog initialization.

Quick start#

Register the delegate once during app startup, before initializing the Diyalog engine or navigating to chat:

import UIKit
import DiyalogEngine
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Register the custom alert delegate to handle all SDK alerts
DiyalogEngine.shared.customAlertDelegate = self
// ... then initialize Diyalog as usual
return true
}
}

To turn the custom alert integration off at runtime, set the delegate back to nil:

DiyalogEngine.shared.customAlertDelegate = nil

1. Custom Alert Dialog#

Brand the confirmation and info dialogs shown by the SDK (such as logout confirmations, error messages and permission prompts) with your app's colors, typography and button styles.

Alert Dialog API#

The SDK exposes a delegate protocol that your app conforms to:

public protocol DiyalogCustomAlertDelegate: AnyObject {
func presentAlert(on viewController: UIViewController,
title: String?,
message: String?,
preferredStyle: Int,
actions: [DLGAlertAction])
}
ParameterDescription
viewControllerThe SDK view controller to present your alert on
titleAlert title; nil when the alert has no title
messageAlert message
preferredStylePreferred presentation style requested by the SDK
actionsDLGAlertAction objects, each with a title and a style (.default, .cancel or .destructive)

Registration#

extension AppDelegate: DiyalogCustomAlertDelegate {
func presentAlert(on viewController: UIViewController, title: String?, message: String?, preferredStyle: Int, actions: [DLGAlertAction]) {
// Instantiate your custom alert view controller
let customAlert = CustomAlertViewController(title: title, message: message, actions: actions)
// Present it over the SDK's view controller
viewController.present(customAlert, animated: true, completion: nil)
}
}

Reference implementation: CustomAlertViewController#

Below is a complete reference implementation of a custom alert view controller. It builds a styled popup with Auto Layout, creates a button for every item in the actions array, and makes sure the right action is invoked when a button is tapped.

import UIKit
import DiyalogEngine
class CustomAlertViewController: UIViewController {
private let titleText: String?
private let messageText: String?
private let actions: [DLGAlertAction]
private let containerView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 12
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private let titleLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(red: 1/255, green: 55/255, blue: 130/255, alpha: 1) // #013782
label.font = UIFont.boldSystemFont(ofSize: 18)
label.textAlignment = .center
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let messageLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(red: 1/255, green: 55/255, blue: 130/255, alpha: 1) // #013782
label.font = UIFont.boldSystemFont(ofSize: 16)
label.textAlignment = .center
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let stackView: UIStackView = {
let stack = UIStackView()
stack.axis = .vertical
stack.spacing = 12
stack.translatesAutoresizingMaskIntoConstraints = false
return stack
}()
init(title: String?, message: String?, actions: [DLGAlertAction]) {
self.titleText = title
self.messageText = message
self.actions = actions
super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .overFullScreen
self.modalTransitionStyle = .crossDissolve
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
setupView()
}
private func setupView() {
view.addSubview(containerView)
containerView.addSubview(titleLabel)
containerView.addSubview(messageLabel)
containerView.addSubview(stackView)
titleLabel.text = titleText
messageLabel.text = messageText
NSLayoutConstraint.activate([
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40),
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40),
titleLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 28),
titleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 24),
titleLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -24),
messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 12),
messageLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 24),
messageLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -24),
stackView.topAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 24),
stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 24),
stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -24),
stackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -24)
])
for action in actions {
let button = UIButton(type: .system)
button.setTitle(action.title.uppercased(), for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.layer.cornerRadius = 24
button.translatesAutoresizingMaskIntoConstraints = false
button.heightAnchor.constraint(equalToConstant: 48).isActive = true
// Style based on the action type (e.g., Cancel vs OK/Default)
if action.style == .cancel {
button.setTitleColor(UIColor(red: 1/255, green: 55/255, blue: 130/255, alpha: 1), for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor(red: 1/255, green: 55/255, blue: 130/255, alpha: 1).cgColor
button.backgroundColor = .clear
} else {
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor(red: 1/255, green: 55/255, blue: 130/255, alpha: 1)
}
// Using ActionWrapper to safely retain and invoke the action handler closure
let actionWrapper = ActionWrapper(action: action, controller: self)
button.addTarget(actionWrapper, action: #selector(ActionWrapper.invoke), for: .touchUpInside)
objc_setAssociatedObject(button, String(format: "[%d]", arc4random()), actionWrapper, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
stackView.addArrangedSubview(button)
}
}
}

Note: ActionWrapper is a small helper class in your app, not part of the SDK. Its invoke method must dismiss the alert view controller and run the handler of the DLGAlertAction it wraps.

Alert Dialog behavior notes#

  • Title and message: passed directly from the SDK based on the context of the alert. If an alert has no title, title is nil.
  • Actions array: contains DLGAlertAction objects, each with a title (e.g. "OK", "Cancel") and a style (such as .default, .cancel or .destructive). Your layout should visually distinguish these styles where needed.
  • Dismissal: the ActionWrapper in the reference implementation (or your equivalent) is responsible for dismissing the view controller with self.dismiss(animated: true), before or after running the SDK's action closure. The SDK does not dismiss your custom UIViewController automatically.

Integration checklist#

Use this before shipping a host customization:

  • The custom delegate is assigned in AppDelegate's didFinishLaunchingWithOptions.
  • Your custom view controller reads the actions array and creates a tap target for every action provided by the SDK.
  • The custom view controller dismisses itself when an action is selected.
  • Trigger a known SDK dialog (e.g. delete a message or trigger a permission prompt) to verify your custom presentation, and check that the SDK completes the action after the button is pressed.