文章目录
1.github地址(亲测好用)
2.源代码
- XHToast.swift
//
// XHToast.swift
// XHToastSwiftExample
//
// Created by xiaohui on 16/8/12.
// Copyright © 2016年 CoderZhuXH. All rights reserved.
// 代码地址:https://github.com/CoderZhuXH/XHToastSwift
import UIKit
/**
* Toast默认停留时间
*/
private let ToastDispalyDuration:CGFloat = 1.2
/**
* Toast到顶端/底端默认距离
*/
private let ToastSpace:CGFloat = 100.0
/**
* Toast背景颜色
*/
private let ToastBackgroundColor = UIColor(red:0.2,green:0.2,blue:0.2,alpha:0.75)
//在window上显示
extension XHToast
{
//MARK:-中间显示
/**
中间显示
- parameter text: 文字
*/
public class func showCenterWithText(_ text: String) {
XHToast.showCenterWithText(text, duration:ToastDispalyDuration)
}
/**
中间显示+自定义时间
- parameter text: 文字
- parameter duration: 自定义停留时间
*/
public class func showCenterWithText(_ text:String,duration:CGFloat) {
let toast = XHToast(text: text)
toast.duration = duration
toast.showIn(UIWindow.window())
}
// MARK:-上方显示
/**
上方显示
- parameter text: 文字
*/
public class func showTopWithText(_ text:String) {
XHToast.showTopWithText(text, topOffset:ToastSpace, duration:ToastDispalyDuration)
}
/**
上方显示+自定义停留时间
- parameter text: 文字
- parameter duration: 自定义停留时间
*/
public class func showTopWithText(_ text:String, duration:CGFloat) {
XHToast.showTopWithText(text, topOffset:ToastSpace, duration:duration)
}
/**
上方显示+自定义到顶部距离
- parameter text: 文字
- parameter topOffset: 自定义到顶部距离
*/
public class func showTopWithText(_ text:String,topOffset:CGFloat) {
XHToast.showTopWithText(text, topOffset:topOffset, duration:ToastDispalyDuration)
}
/**
上方显示+自定义到顶部距离+自定义停留时间
- parameter text: 文字
- parameter topOffset: 自定义到顶部距离
- parameter duration: 自定义停留时间
*/
public class func showTopWithText(_ text:String, topOffset:CGFloat,duration:CGFloat) {
let toast = XHToast(text: text)
toast.duration = duration
toast.showIn(UIWindow.window(), topOffset: topOffset)
}
// MARK:-下方显示
/**
下方显示
- parameter text: 文字
*/
public class func showBottomWithText(_ text:String)