Custom Toast, Alert and PopUps (HUDs) in swift

SwiftNotice is a GUI library for displaying various popups (HUD) written in pure Swift, fits any scrollview and supports iPhone.

Top Best Basic Augmented Reality Apps for iOS
augmented reality app
ar app
ar ios app development

Installation

Just clone and add SwiftNotice.swift to your project.

How to use

self.pleaseWait()

self.noticeTop("OK!")

self.noticeSuccess("Success!")
self.noticeError("Error!")
self.noticeInfo("Info")

self.noticeOnlyText("Only Text")

self.clearAllNotice() // clear

use custom images in self.pleaseWait()

var imagesArray = Array<UIImage>()
for i in 1...7 {
    imagesArray.append(UIImage(named: "loading\(i)")!)
}
self.pleaseWaitWithImages(imagesArray, timeInterval: 50)

make it auto-clear

self.noticeTop("OK!", autoClear: true)

self.noticeSuccess("Success!", autoClear: true)
self.noticeError("Error!", autoClear: true)
self.noticeInfo("Info", autoClear: true)

auto-clear and set auto-clear-time

self.noticeTop("OK!", autoClear: true, autoClearTime: 1)

self.noticeSuccess("Success!", autoClear: true, autoClearTime: 10)
self.noticeError("Error!", autoClear: true, autoClearTime: 10)
self.noticeInfo("Info", autoClear: true, autoClearTime: 10)

old api with full power

self.notice("Success!", type: NoticeType.success, autoClear: true)
self.notice("Error!", type: NoticeType.error, autoClear: true)
self.notice("Info", type: NoticeType.info, autoClear: true)

use SwiftNotice anywhere

SwiftNotice.wait()
SwiftNotice.noticeOnSatusBar("noticeOnSatusBar", autoClear: true, autoClearTime: 1)
SwiftNotice.showText("showText")
SwiftNotice.showNoticeWithText(NoticeType.success, text:"showNoticeWithText", autoClear: true, autoClearTime: 3)
SwiftNotice.clear()

Download

SwiftNotice

Other Way to Create Custom Alert, Toast and Popup in swift

In Swift, you can create custom toast messages, alerts and popups (HUDs) by designing custom views and presenting them when needed. I will provide examples for each:

Custom Toast:

A toast is a non-modal message that appears temporarily and then disappears automatically after a few seconds. To create a custom toast, you can design a custom view and present it using a UIView animation.

import UIKit

class CustomToastView: UIView {
    // Your custom toast view design and content here
}

extension UIViewController {
    func showToast(message: String) {
        let toastView = CustomToastView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
        toastView.center = view.center
        toastView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        toastView.layer.cornerRadius = 10
        toastView.clipsToBounds = true

        let toastLabel = UILabel(frame: toastView.bounds)
        toastLabel.textAlignment = .center
        toastLabel.textColor = .white
        toastLabel.text = message
        toastView.addSubview(toastLabel)

        view.addSubview(toastView)

        UIView.animate(withDuration: 2.0, delay: 1.0, options: .curveEaseOut, animations: {
            toastView.alpha = 0.0
        }, completion: { _ in
            toastView.removeFromSuperview()
        })
    }
}

To show the toast from any view controller, simply call showToast with the message you want to display:

self.showToast(message: "This is a custom toast message!")

Custom Popup (HUD):

A popup or HUD (Heads-Up Display) is a modal view that typically appears in the center of the screen to display important information or loading progress. Create a custom popup view and present it modally.

import UIKit

class CustomPopupView: UIView {
    // Your custom popup view design and content here
}

extension UIViewController {
    func showPopup() {
        let popupView = CustomPopupView(frame: CGRect(x: 0, y: 0, width: 200, height: 150))
        popupView.center = view.center
        popupView.backgroundColor = .white
        popupView.layer.cornerRadius = 10
        popupView.clipsToBounds = true

        view.addSubview(popupView)

        // Optionally, you can add a close button to dismiss the popup
        let closeButton = UIButton(frame: CGRect(x: 10, y: 10, width: 30, height: 30))
        closeButton.setTitle("X", for: .normal)
        closeButton.addTarget(self, action: #selector(closePopup), for: .touchUpInside)
        popupView.addSubview(closeButton)
    }

    @objc func closePopup() {
        view.subviews.filter { $0 is CustomPopupView }.forEach { $0.removeFromSuperview() }
    }
}

To show the popup from any view controller, simply call showPopup:

self.showPopup()

Custom Alert:

A custom alert can be created by designing a custom view and presenting it as a modal view controller.

import UIKit

class CustomAlertView: UIView {
    // Your custom alert view design and content here
}

extension UIViewController {
    func showAlert() {
        let alertView = CustomAlertView(frame: CGRect(x: 0, y: 0, width: 200, height: 150))
        alertView.center = view.center
        alertView.backgroundColor = .white
        alertView.layer.cornerRadius = 10
        alertView.clipsToBounds = true

        view.addSubview(alertView)

        // Optionally, you can add a close button to dismiss the alert
        let closeButton = UIButton(frame: CGRect(x: 10, y: 10, width: 30, height: 30))
        closeButton.setTitle("X", for: .normal)
        closeButton.addTarget(self, action: #selector(closeAlert), for: .touchUpInside)
        alertView.addSubview(closeButton)
    }

    @objc func closeAlert() {
        view.subviews.filter { $0 is CustomAlertView }.forEach { $0.removeFromSuperview() }
    }
}

To show the custom alert from any view controller, simply call showAlert:

self.showAlert()

Customise the CustomToastView, CustomPopupView, and CustomAlertView classes is basic example.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *