gpt4 book ai didi

ios - 如何在 Swift 中创建 toast 消息?

转载 作者:IT王子 更新时间:2023-10-29 04:59:17 24 4
gpt4 key购买 nike

有什么方法可以在 swift 中制作 toast 消息吗?

我在 objective c 中尝试过,但无法在 swift 中找到解决方案。

[self.view makeToast:@"Account created Successfully"
duration:0.5
position:@"bottom"];

最佳答案

extension UIViewController {

func showToast(message : String, font: UIFont) {

let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.font = font
toastLabel.textAlignment = .center;
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
} }

像这样使用:

self.showToast(message: "Your Toast Message", font: .systemFont(ofSize: 12.0))

关于ios - 如何在 Swift 中创建 toast 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com