Skip to content

bluesky335/Toaster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🍞 Toaster

A toaster library for iOS written in Swift.

Feature

  • πŸ€– Android-like Toast.
  • πŸ“¦ Ready-to-use right out of the box.
  • βœ‚οΈ Easy to customize.
  • 🐦 Swift package manager supported.
  • ⌨️ Not blocked by the keyboard.
  • 🍎 iOS 11 or newer is support.

Screen record

studio_video_1679933979987464.MP4

Screenshot

Usage

install

To integrate Toaster into your Xcode project using Swift Package Manager, add it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/bluesky335/Toaster.git", .upToNextMajor(from: "0.2.0"))
]

or using Xcode swift package manager:

xcode setting then add 'Toaster' in to 'Frameworks,Libraries,and Embedded Content': xcode setting

using in code

Toast(text:"🍞 o(οΏ£β–½οΏ£)d ").show()

How to customized

simple customize

// customize toast style
Toast.successStyle = .init(backgroundColor: .systemGreen.withAlphaComponent(0.8))
Toast.errorStyle = .init(backgroundColor: .systemRed.withAlphaComponent(0.8))
Toast.warningStyle = .init(backgroundColor: .systemYellow.withAlphaComponent(0.8))
Toast.defaultStyle = .init()
// customize toast position layout or fixedWidth
ToastCenter.default.toastAnimator = ToastAnimatter(position: .top, layout: .stack, fixedWidth: true)

full customize

  • implement you own ToastType

    Optional step, you can use the default implementation: Toast

    public struct MyToast: ToastType {
        public var duration: ToastDuration
        public var content: String
    }
  • implement a ToastViewProviderType for your ToastType

    public final class MyToastViewProvider: ToastViewProviderType<MyToast> {
      @MainActor override public func viewForToast(toast: MyToast) -> UIView {
          let view = UIView()
          /*
          .... 
          Create a view and set it via the "toast" parameter.
          This view will be displayed by toast center.
          ....
          */
          return view
      }
    }
  • register your ToastViewProviderType into toast center

    // If this type of toast is already registered, this will replace it.
    ToastCenter.default.register(toastType: MyToast.self, with: MyToastViewProvider())
  • show your toast

    MyToast(duration:.short, content: "toast text").show()
    

you can also customize the toast animation by implement your own ToastAnimatterType and set it to ToastCenter.default.toastAnimator replacing the default ToastAnimatter.

multiple scene support

ToastCenter.default display the toast view in the first UIWindowScene connected to the app.

you can create ToastCenter for diffrent window scenes by ToastCenter(windowScene: scene).

then using Toast(text:"toast text").show(in: center) to show toast.