如何在Swift中敬酒信息?

有没有什么办法在迅速吐司消息?

我已经在客观的C尝试,但无法迅速find解决scheme。

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

 extension UIViewController { func showToast(message : String) { 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.textAlignment = .center; toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0) 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() }) } } 

只需添加下面的方法。 这将以不同的颜色显示消息(消息从左到右&消失)。

Swift 3.0 –

 class Toast { class private func showAlert(backgroundColor:UIColor, textColor:UIColor, message:String) { let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate let label = UILabel(frame: CGRect.zero) label.textAlignment = NSTextAlignment.center label.text = message label.font = UIFont(name: "", size: 15) label.adjustsFontSizeToFitWidth = true label.backgroundColor = backgroundColor //UIColor.whiteColor() label.textColor = textColor //TEXT COLOR label.sizeToFit() label.numberOfLines = 4 label.layer.shadowColor = UIColor.gray.cgColor label.layer.shadowOffset = CGSize(width: 4, height: 3) label.layer.shadowOpacity = 0.3 label.frame = CGRect(x: appDelegate.window!.frame.size.width, y: 64, width: appDelegate.window!.frame.size.width, height: 44) label.alpha = 1 appDelegate.window!.addSubview(label) var basketTopFrame: CGRect = label.frame; basketTopFrame.origin.x = 0; UIView.animate(withDuration :2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in label.frame = basketTopFrame }, completion: { (value: Bool) in UIView.animate(withDuration:2.0, delay: 2.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in label.alpha = 0 }, completion: { (value: Bool) in label.removeFromSuperview() }) }) } class func showPositiveMessage(message:String) { showAlert(backgroundColor: UIColor.green, textColor: UIColor.white, message: message) } class func showNegativeMessage(message:String) { showAlert(backgroundColor: UIColor.red, textColor: UIColor.white, message: message) } } 

你到底需要什么是https://github.com/Rannie/Toast-Swift/blob/master/SwiftToastDemo/Toast/HRToast%2BUIView.swift

下载HRToast + UIView.swift类并拖放到项目中。 确保在对话框中选中“需要时复制项目”。

  //Usage: self.view.makeToast(message: "Simple Toast") self.view.makeToast(message: "Simple Toast", duration: 2.0, position:HRToastPositionTop) self.view.makeToast(message: "Simple Toast", duration: 2.0, position: HRToastPositionCenter, image: UIImage(named: "ic_120x120")!) self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionDefault, title: "Simple Toast") self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionCenter, title: "Simple Toast", image: UIImage(named: "ic_120x120")!) self.view.makeToastActivity() self.view.makeToastActivity(position: HRToastPositionCenter) self.view.makeToastActivity(position: HRToastPositionDefault, message: "Loading") self.view.makeToastActivityWithMessage(message: "Loading") 

有一个第三方库支持单行代码的可定制Toast通知。 这是一个简单的例子:

 import Toast_Swift ... // basic usage self.view.makeToast("This is a piece of toast") // toast with a specific duration and position self.view.makeToast("This is a piece of toast", duration: 3.0, position: .top) 

https://github.com/scalessec/Toast-Swift

(更新了Swift 3)

如果makeToast:duration:position:在objective-c中定义并且可以被调用,那么swift代码将会是

 self.view.makeToast("Acount created Successfully", duration: 0.5, position: "bottom") 

尽pipe你可能需要使用桥接头来访问你的swift代码中的这些方法。

@ mr-bean代码更新到最新的Swift版本(3.x)

  let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 150, y: self.view.frame.size.height-100, width: 300, height: 35)) toastLabel.backgroundColor = UIColor.black toastLabel.textColor = UIColor.white toastLabel.textAlignment = NSTextAlignment.center self.view.addSubview(toastLabel) toastLabel.text = message toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10; toastLabel.clipsToBounds = true UIView.animate(withDuration: 4.0, animations: { toastLabel.alpha = 0.0 }) 

斯威夫特4

 func showToast(message : String) { 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.textAlignment = .center; toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0) 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: "Data Save.") 

而不是使用UILabel使用UITextView会得到更好的结果。

 func showToast(message: String) { let toastLabel = UITextView(frame: CGRect(x: self.view.frame.size.width/16, y: self.view.frame.size.height-150, width: self.view.frame.size.width * 7/8, height: 35)) toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6) toastLabel.textColor = UIColor.white toastLabel.textAlignment = .center; toastLabel.text = " \(message) " toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10; toastLabel.clipsToBounds = true toastLabel.font = UIFont(name: (toastLabel.font?.fontName)!, size: 16) toastLabel.layoutEdgeInsets.left = 8 toastLabel.layoutEdgeInsets.right = 8 toastLabel.center.x = self.view.frame.size.width/2 self.view.addSubview(toastLabel) UIView.animate(withDuration: 5.0, delay: 0.1, options: .curveEaseOut, animations: { toastLabel.alpha = 0.0 }, completion: {(isCompleted) in toastLabel.removeFromSuperview() }) } 

在空格中添加消息,以便在两端提供良好的间距,使其看起来不错。 Mr.Bean答案的修改版

 static func popUp(context ctx: UIViewController, msg: String) { let toast = UILabel(frame: CGRect(x: 16, y: ctx.view.frame.size.height / 2, width: ctx.view.frame.size.width - 32, height: 100)) toast.backgroundColor = UIColor.lightGray toast.textColor = UIColor.white toast.textAlignment = .center; toast.numberOfLines = 3 toast.font = UIFont.systemFont(ofSize: 20) toast.layer.cornerRadius = 12; toast.clipsToBounds = true toast.text = msg ctx.view.addSubview(toast) UIView.animate(withDuration: 5.0, delay: 0.2, options: .curveEaseOut, animations: { toast.alpha = 0.0 }, completion: {(isCompleted) in toast.removeFromSuperview() }) } 

然后从UIViewController调用它

 popUp(context: self, msg: "Your message") 

我知道有些答案是可以接受的,但是它们似乎都有一个很大的缺陷 – 如果你在很短的时间内performance出几个敬酒,那么它们将会相互展现。 这是我的实现,考虑到这个问题:

 class Toast: UILabel { private let BOTTOM_MARGIN: CGFloat = 16 private let SIDE_MARGIN: CGFloat = 16 private let HEIGHT: CGFloat = 35 private let SHOW_TIME_SECONDS = TimeInterval(3) private let BACKGROUND_COLOR = UIColor.darkGray.withAlphaComponent(0.7).cgColor private let TEXT_COLOR = UIColor.white private let ANIMATION_DURATION_SEC = 0.33 private static var queue: [ToastHolder] = [] private static var showing: Toast? init(_ text: String) { super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) self.text = text self.textColor = TEXT_COLOR textAlignment = .center self.layer.backgroundColor = BACKGROUND_COLOR self.layer.cornerRadius = 5 } public func show(_ parent: UIViewController) { frame = CGRect(x: SIDE_MARGIN, y: UIScreen.main.bounds.height - BOTTOM_MARGIN - HEIGHT, width: UIScreen.main.bounds.width - 2 * SIDE_MARGIN, height: HEIGHT) if Toast.showing == nil { Log.d("showing \(String(describing: text))") Toast.showing = self alpha = 0 parent.view.addSubview(self) UIView.animate(withDuration: ANIMATION_DURATION_SEC, animations: { self.alpha = 1 }, completion: { (completed) in Timer.scheduledTimer(timeInterval: self.SHOW_TIME_SECONDS, target: self, selector: #selector(self.onTimeout), userInfo: nil, repeats: false) }) } else { Toast.queue.append(ToastHolder(self, parent)) } } @objc func onTimeout() { UIView.animate(withDuration: ANIMATION_DURATION_SEC, animations: { self.alpha = 0 }, completion: { (completed) in Toast.showing = nil self.removeFromSuperview() if !Toast.queue.isEmpty { let holder = Toast.queue.removeFirst() holder.toast.show(holder.parent) } }) } required init?(coder aDecoder: NSCoder) { fatalError("this initializer is not supported") } private class ToastHolder { let toast: Toast let parent: UIViewController init(_ t: Toast, _ p: UIViewController) { toast = t parent = p } } } 

用法:

 Toast("my message").show(self) 

希望它可以帮助别人。