如何使用 swift 在应用程序委托函数中使用 toast 方法
How to use toast method in app delegate functions using swift
您好,我正在我的应用程序中使用 swift 开发应用程序,我想使用 toast 消息和 toast activity,所以我遵循了 link:https://github.com/scalessec/Toast-Swift。我可以在视图控制器方法中使用它运行良好,但我不能在应用程序委托方法中使用。
我的应用委托中的代码:
func loadJsonData(){
self.view.makeToastActivity(.center)
}
上面提到的代码不起作用,因为 app delegate 没有成员视图...请帮助我在我的 app delegate 中使用它。
请试试这个,通过这个你可以得到你的应用程序的顶部控制器,然后你可以在顶部控制器上添加 toast
let win:UIWindow = UIApplication.shared.delegate!.window!!
win.currentViewController()?.view
AppDelegate 用于处理初始化应用程序、关闭应用程序、通知等事情
您想做的是:
- 转到故事板(名为 Main.storyboard)
- 添加一个 ViewController 到故事板 (drag it from the bottom right)
- 创建一个Swift文件并命名为FirstView,例如添加如下代码
FirstView.swift
import UIKit
class FirstView: UIViewController
{
override func viewDidLoad()
{
self.view.makeToastActivity(.center);
}
}
- 返回故事板
- 点击您刚刚创建的ViewController
- 看屏幕右上角,有六个小图标。单击左侧的三分之一,然后在名为 "Class" (see attached picture).
的第一个字段中键入 FirstView
注意:请务必保存 FirstView.swift 文件,否则将无法正常工作。
定制的 Toast 怎么样?一种更诱人,适合您的需要并且不需要图书馆或复杂的含义?
现在让我们试试下面的代码
func sailAwayLabelAction(){
// here creating a rectangle with certain dimensions you can easily manipulate
let rect = CGRect(origin: CGPoint(x: self.view.frame.size.width/2 - 150,y :self.view.frame.size.height-100), size: CGSize(width: 300, height: 35))
//here creating and manipulating the attributes of your text, i.e color,alignment etc..
let toastLabel = UILabel(frame: rect)
toastLabel.backgroundColor = UIColor.orange
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = NSTextAlignment.center;
toastLabel.text = "This is my customized Toast !"
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
//first pop the toast into our view
self.view.addSubview(toastLabel)
//then after 1 sec + 1 sec delay, animate the entire toastLabel out.
UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
})
}
每当你激活之前的功能时,它应该呈现类似这样的东西,
您好,我正在我的应用程序中使用 swift 开发应用程序,我想使用 toast 消息和 toast activity,所以我遵循了 link:https://github.com/scalessec/Toast-Swift。我可以在视图控制器方法中使用它运行良好,但我不能在应用程序委托方法中使用。
我的应用委托中的代码:
func loadJsonData(){
self.view.makeToastActivity(.center)
}
上面提到的代码不起作用,因为 app delegate 没有成员视图...请帮助我在我的 app delegate 中使用它。
请试试这个,通过这个你可以得到你的应用程序的顶部控制器,然后你可以在顶部控制器上添加 toast
let win:UIWindow = UIApplication.shared.delegate!.window!!
win.currentViewController()?.view
AppDelegate 用于处理初始化应用程序、关闭应用程序、通知等事情
您想做的是:
- 转到故事板(名为 Main.storyboard)
- 添加一个 ViewController 到故事板 (drag it from the bottom right)
- 创建一个Swift文件并命名为FirstView,例如添加如下代码
FirstView.swift
import UIKit
class FirstView: UIViewController
{
override func viewDidLoad()
{
self.view.makeToastActivity(.center);
}
}
- 返回故事板
- 点击您刚刚创建的ViewController
- 看屏幕右上角,有六个小图标。单击左侧的三分之一,然后在名为 "Class" (see attached picture). 的第一个字段中键入 FirstView
注意:请务必保存 FirstView.swift 文件,否则将无法正常工作。
定制的 Toast 怎么样?一种更诱人,适合您的需要并且不需要图书馆或复杂的含义?
现在让我们试试下面的代码
func sailAwayLabelAction(){
// here creating a rectangle with certain dimensions you can easily manipulate
let rect = CGRect(origin: CGPoint(x: self.view.frame.size.width/2 - 150,y :self.view.frame.size.height-100), size: CGSize(width: 300, height: 35))
//here creating and manipulating the attributes of your text, i.e color,alignment etc..
let toastLabel = UILabel(frame: rect)
toastLabel.backgroundColor = UIColor.orange
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = NSTextAlignment.center;
toastLabel.text = "This is my customized Toast !"
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
//first pop the toast into our view
self.view.addSubview(toastLabel)
//then after 1 sec + 1 sec delay, animate the entire toastLabel out.
UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
})
}
每当你激活之前的功能时,它应该呈现类似这样的东西,