2017-01-02 88 views
2

在android中,我们可以直接添加烤面包。 他们有什么方法在iOS中添加类似的烤面包?如何在iOS中添加android像烤面包一样?

我创建了透明视图用作吐司,但对于多个文本大小,我必须创建多个视图。

+3

这里有很多:https://www.cocoacontrols.com/search?q =吐司 –

+0

还有什么你需要用android在这里,亲切更新。 – vaibhav

回答

3

有iOS中没有可用的Android类型吐司控制。

如果你想使用类似的东西,你需要自定义的UIView用的UILabel,或使用一些已经创建吐司类型的组件,如下图所示:

Android Type Toast custom

0

有一个第三方库支持单行代码的可定制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) 

Toast Swift library

否则,

如果你想通过自己来实现。使用下面的代码。

let toastLabel = UILabel(frame: CGRectMake(self.view.frame.size.width/2 - 150, self.view.frame.size.height-100, 300, 35)) 
        toastLabel.backgroundColor = UIColor.blackColor() 
        toastLabel.textColor = UIColor.whiteColor() 
        toastLabel.textAlignment = NSTextAlignment.Center; 
        self.view.addSubview(toastLabel) 
        toastLabel.text = "hello man..." 
        toastLabel.alpha = 1.0 
        toastLabel.layer.cornerRadius = 10; 
        toastLabel.clipsToBounds = true 
        UIView.animateWithDuration(4.0, delay: 0.1, options: UIViewAnimationOptions.CurveEaseOut, animations: { 

         toastLabel.alpha = 0.0 

         }) 
1

您可以使用MBProgressHUD展现出像android一样敬酒。加入后MBProgressHUD您可以通过这种方式

 let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true) 
     progressHUD.mode = MBProgressHUDMode.text 
     progressHUD.detailsLabel.text = "Your message here" 
     progressHUD.margin = 10.0 
     progressHUD.offset.y = 150.0 
     progressHUD.isUserInteractionEnabled = false 
     progressHUD.removeFromSuperViewOnHide = true 
     progressHUD.hide(animated: true, afterDelay: 3.0) 
0

显示举杯您可以使用此功能在iOS中显示敬酒消息。只要创建视图上的延伸和调用此方法与消息

extension UIView { 

    func displayToast(message : String) -> Void { 

      let toastView = UILabel() 
      toastView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7) 
      toastView.textColor = UIColor.whiteColor() 
      toastView.textAlignment = .Center 
      toastView.font = UIFont(name: "Font-name", size: 17) 
      toastView.cornerRadius = 25 
      toastView.text = message 
      toastView.numberOfLines = 0 
      toastView.alpha = 0 
      toastView.translatesAutoresizingMaskIntoConstraints = false 

      let window = UIApplication.sharedApplication().delegate?.window! 
      window?.addSubview(toastView) 

      let horizontalCenterContraint: NSLayoutConstraint = NSLayoutConstraint(item: toastView, attribute: .CenterX, relatedBy: .Equal, toItem: window, attribute: .CenterX, multiplier: 1, constant: 0) 

      let widthContraint: NSLayoutConstraint = NSLayoutConstraint(item: toastView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: (self.frame.size.width-25)) 

      let verticalContraint: [NSLayoutConstraint] = NSLayoutConstraint.constraintsWithVisualFormat("V:|-(>=200)-[loginView(==50)]-68-|", options: [.AlignAllCenterX, .AlignAllCenterY], metrics: nil, views: ["loginView": toastView]) 

      NSLayoutConstraint.activateConstraints([horizontalCenterContraint, widthContraint]) 
      NSLayoutConstraint.activateConstraints(verticalContraint) 

      UIView.animateWithDuration(0.5, delay: 0, options: .CurveEaseIn, animations: { 
       toastView.alpha = 1 
      }, completion: nil) 

      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), { 
       UIView.animateWithDuration(0.5, delay: 0, options: .CurveEaseIn, animations: { 
        toastView.alpha = 0 
       }, completion: { finished in 
        toastView.removeFromSuperview() 
       }) 
      }) 
     } 
} 

用法:

只是调用从视图控制器self.view.displayToastMessage("message")

0

This是我用来显示iOS的面包最好的图书馆应用程序相同作为android。

它还具有pod支持和POD的名字是pod 'Toast'

和实施类似

#import <UIView+Toast.h> 
ViewController

,然后下面的行那么简单,无论你想显示它

[self.view makeToast:@"YOUR TOAST MESSAGE" duration:TOAST_TIMEOUT position:TOAST_CENTER]; 

以上按键值为

#define TOAST_TOP @"CSToastPositionTop" 
#define TOAST_CENTER @"CSToastPositionCenter" 
#define TOAST_BOTTOM @"CSToastPositionBottom" 
#define TOAST_TIMEOUT 2.0