测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年9月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✗ |
Maintained by Daniele Boscolo.
为Swift定制的iOS吐司视图
要运行示例项目,请克隆仓库,并从“Example”目录中打开SwiftToast.xcworkspace
。
您可以选择三种样式之一
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)
要使用自定义.xib
,您必须将您的类实现为SwiftToastViewProtocol
protocol SwiftToastViewProtocol: class {
func nib() -> SwiftToastViewProtocol?
func configure(with toast: SwiftToastProtocol)
}
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
// }
}
}
}
要轻松地呈现自定义吐司视图
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)
isUserInteractionEnabled
为true
,并且用户在吐司内触摸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文件。