SwiftToast 1.1.3

SwiftToast 1.1.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年9月
SwiftSwift版本3.0
SPM支持SPM

Maintained by Daniele Boscolo.



  • By
  • damboscolo

SwiftToast





为Swift定制的iOS吐司视图

示例

要运行示例项目,请克隆仓库,并从“Example”目录中打开SwiftToast.xcworkspace

要求

  • Swift 3
  • iOS 9.0或更高

使用

吐司样式

您可以选择三种样式之一

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

呈现

简单的动画呈现

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

消失

自动

SwiftToast消失是由属性duration控制的。当时间到了,吐司会自动消失。
另一个控制属性是isUserInteractionEnabled,默认值为true。当用户触摸到吐司上时,它会自动消失。

程序性地

如果您想手动消失吐司,可以将isUserInteractionEnabled设置为false,并将duration设置为nil

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

并且要消失

dismissSwiftToast(true)

自定义

SwiftToast非常易于自定义。您可以在创建SwiftTost对象时进行自定义。如下代码所示

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)

它生成了这个

或者您还可以更改默认值,包括文本,因此所有未来显示的吐司都将看起来相同

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)

自定义SwiftToastView

要使用自定义.xib,您必须将您的类实现为SwiftToastViewProtocol

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

自定义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
            // }
        }
    }
}

呈现自定义SwiftToastView

要轻松地呈现自定义吐司视图

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)

代理

SwiftToastDelegate有3个可选代理。它们在属性target上设置,并在以下情况下调用

  • 返回所显示的SwiftToast的高度
func swiftToast(_ swiftToast: SwiftToastProtocol, presentedWith height: CGFloat)
  • 消失的SwiftToast
func swiftToastDismissed(_ swiftToast: SwiftToastProtocol)
  • 如果isUserInteractionEnabledtrue,并且用户在吐司内触摸
func swiftToastDidTouchUpInside(_ swiftToast: SwiftToastProtocol)

配置

为了使statusBarStyle工作,您必须将项目中的View controller-based status bar appearance行添加到Info.plist中,并将其设置为NO

默认值

属性 类型 默认值 定义
text String “” 吐司文本
textAlignment NSTextAlignment .center 文本对齐
backgroundColor UIColor .red 背景颜色
textColor UIColor .white 文本颜色
image UIImage nil 吐司图标
font UIFont .boldSystemFont(ofSize: 14.0) 文本字体
duration Double 2.0 消失持续时间。如果为nil,则不自动消失
minimumHeight CGFloat nil 吐司的最小高度。如果为nil,则使用动态最小高度
statusBarStyle UIStatusBarStyle .lightContent 吐司出现时的状态栏样式
aboveStatusBar Bool false 在吐司出现时显示/隐藏状态栏
isUserInteractionEnabled Bool false 如果为真,则点击消失。如果为假,则点击不消失
目标 SwiftToastDelegate nil 点击吐司代理
样式 SwiftToastStyle .navigationBar navigationBar, statusBar或bottomToTop样式

作者

damboscolo,[email protected]

许可证

SwiftToast遵循MIT许可证。有关更多信息,请参阅LICENSE文件。