GithubHelp home page GithubHelp logo

damboscolo / swifttoast Goto Github PK

View Code? Open in Web Editor NEW
37.0 3.0 14.0 3.43 MB

๐Ÿž All customizable iOS toast view in Swift

Home Page: https://damboscolo.github.io/SwiftToast

License: MIT License

Ruby 1.92% Objective-C 2.10% Shell 37.11% Swift 58.87%

swifttoast's Introduction

SwiftToast

CI Status Platform Version Carthage Compatible

A customizable iOS toast view for Swift

Example

To run the example project, clone the repo and open SwiftToast.xcworkspace from the Example directory.

Requirements

  • Swift 3
  • iOS 9.0 or higher

Installation

CocoaPods

SwiftToast is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftToast"

Carthage

Simply add the following lines to your Cartfile:

github 'damboscolo/SwiftToast'

Usage

Toast Style

You may choose between three styles

enum SwiftToastStyle {
    case navigationBar
    case statusBar
    case bottomToTop
    case belowNavigationBar
}

To present

A simple animated presentation:

let test = SwiftToast(text: "This is a Toast")
present(toast, animated: true)

Dismiss

Automatically

The SwiftToast dismiss is controlled by the attribute duration. When it's over, the toast automatically dismiss. Another attribute that control is isUserInteractionEnabled that is true by default. When user touch above the toast it will automatically dismiss.

Programatically

If you want to manually dismiss the toast, you can set isUserInteractionEnabled to false and duration as nil:

let test =  SwiftToast(text: "This is a SwiftToast",
                       duration: nil,
                       isUserInteractionEnabled: false)
present(toast, animated: true)

And to dismiss:

dismissSwiftToast(true)

Customization

SwiftToast is very customizable. You may customize when you create SwiftTost object. Like this code:

let test =  SwiftToast(
                    text: "This is a customized SwiftToast with image",
                    textAlignment: .left,
                    image: UIImage(named: "ic_alert"),
                    backgroundColor: .purple,
                    textColor: .white,
                    font: .boldSystemFont(ofSize: 15.0),
                    duration: 2.0,
                    minimumHeight: CGFloat(100.0),
                    statusBarStyle: .lightContent,
                    aboveStatusBar: true,
                    target: nil,
                    style: .navigationBar)
present(toast, animated: true)

That generates this

Or you can change the default values, even the text, so all future presented toasts will look the same:

SwiftToast.defaultValue.text = "This is a Toast"
SwiftToast.defaultValue.backgroundColor = .green
SwiftToast.defaultValue.image = UIImage(named: "ic_alert")
SwiftToast.defaultValue.style = .bottomToTop

let toast = SwiftToast(text: "This is another Toast")
present(toast, animated: true)

Custom SwiftToastView

To use a custom .xib, you have to implement your as class as SwiftToastViewProtocol

protocol SwiftToastViewProtocol: class {
    func nib() -> SwiftToastViewProtocol?
    func configure(with toast: SwiftToastProtocol)
}

Basics class for a custom SwiftToastView

struct CustomSwiftToast: SwiftToastProtocol {
    // Protocoled
    var duration: Double?
    var minimumHeight: CGFloat?
    var aboveStatusBar: Bool
    var statusBarStyle: UIStatusBarStyle
    var isUserInteractionEnabled: Bool
    var target: SwiftToastDelegate?
    var style: SwiftToastStyle

    // Customized
    var title: String
    var subtitle: String
    var backgroundColor: UIColor
}

class CustomSwiftToastView: UIView, SwiftToastViewProtocol {

    // Customized
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!
    @IBOutlet weak var viewMinimumHeightConstraint: NSLayoutConstraint!

    // Protocoled
    func nib() -> SwiftToastViewProtocol? {
        return Bundle.main.loadNibNamed("CustomSwiftToastView", owner: self, options: nil)?.first as? CustomSwiftToastView
    }

    func configure(with toast: SwiftToastProtocol) {
        if let customToast = toast as? CustomSwiftToast {

            // Put your configure code here. e.g.:

            // subtitleLabel.text = customToast.subtitle
            // backgroundColor = customToast.backgroundColor

            // Setup minimum height if needed

            // if let minimumHeight = toast.minimumHeight {
            //    viewMinimumHeightConstraint.constant = minimumHeight
            // }
        }
    }
}

Present a custom SwiftToastView

To easily present a custom toast view:

let customToast = CustomSwiftToast(
                    duration: 3.0,
                    minimumHeight: nil,
                    aboveStatusBar: true,
                    statusBarStyle: .lightContent,
                    isUserInteractionEnabled: true,
                    target: nil,
                    style: .navigationBar,
                    title: "CUSTOM VIEW",
                    subtitle: "This is a totally customized subtitle",
                    backgroundColor: .blue
                    )
present(customToast, withCustomSwiftToastView: CustomSwiftToastView(), animated: true)

Delegates

SwiftToastDelegate has 3 optional delegates. They are set on the property target and are called when:

  • Return the presented SwiftToast height:
func swiftToast(_ swiftToast: SwiftToastProtocol, presentedWith height: CGFloat)
  • Dismissed SwiftToast:
func swiftToastDismissed(_ swiftToast: SwiftToastProtocol)
  • If isUserInteractionEnabled are true and the user touched up inside the toast:
func swiftToastDidTouchUpInside(_ swiftToast: SwiftToastProtocol)

Configuration

To statusBarStyle works, you have to add row View controller-based status bar appearance on your Info.plist project and set to NO.

Default values

Property Type Default value Definition
text String "" Toast text
textAlignment NSTextAlignment .center Text alignment
backgroundColor UIColor .red Background color
textColor UIColor .white Text color
image UIImage nil Toast icon
font UIFont .boldSystemFont(ofSize: 14.0) Text font
duration Double 2.0 Duration to dismiss. If nil, don't dismiss automatically
minimumHeight CGFloat nil Minimum toast's height. If nil, uses dynamic minimum height
statusBarStyle UIStatusBarStyle .lightContent Status bar style during toast appearance
aboveStatusBar Bool false Show/Hide status bar during toast appearance
isUserInteractionEnabled Bool false If true, dismiss on click. If false, don't dismiss on click
target SwiftToastDelegate nil Click on toast delegate
style SwiftToastStyle .navigationBar navigationBar, statusBar or bottomToTop style

Author

damboscolo, [email protected]

License

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

swifttoast's People

Contributors

damboscolo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swifttoast's Issues

Type 'SwiftToastStyle?' has no member 'aboveNavigationBar'

Hi Daniele,

Thanks for making SwiftToast!

I was looking for a way to display the toast right below the navigation bar. Reading your documentation it seems like the SwiftToastStyle I was looking for is .aboveNavigationBar.
First thing is I don't quite understand the naming of this style as the toast clearly is presented below the navigation bar.
Second thing is that using the pod install, and then trying to use the .aboveNavigationBar style the swift compiler throws an error:
Type 'SwiftToastStyle?' has no member 'aboveNavigationBar'

Eventually for this style I would love to be able to set a minimal height for the toast view that is for example 44pts.

Thanks again and have a great weekend :-)

Joss.

Hidden Because of Keyboard

Thanks for amazing work.

Now I am facing issue , I have to use bottomToTop for type. but Toast is hidden beneath the keyboard.

So Is there any way to fix this. ?

Status bar is disappearing

When I am using Swift Toast style .statusBar and "above status bar". Toast showing up over status bar. It's OK. But when toast is going away, there is no status bar. And later status bar showing up. How to make that status bar was always visible?

Getting errors on Xcode version 14.2

Hi @damboscolo

First of all thank you so much for this wonderful repository. I have tried your library and getting errors on Xcode 14.2.

If possible can you please update your code to support for latest Xcode version.

Thank you.

About iOS 8.0 compatibility

Here the error:

Class Unavailable:
UIStackView before iOS 9.0
SwiftToastView.xib

Can alternative xib to programmatically?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.