アンドロイドではトーストを直接追加できます。 iOSに類似のトーストを追加する方法はありますか?iOSでトーストのようにアンドロイドを追加するには?
私はトーストとして使用するために透明なビューを作成しましたが、複数のテキストサイズの場合、複数のビューを作成する必要があります。
アンドロイドではトーストを直接追加できます。 iOSに類似のトーストを追加する方法はありますか?iOSでトーストのようにアンドロイドを追加するには?
私はトーストとして使用するために透明なビューを作成しましたが、複数のテキストサイズの場合、複数のビューを作成する必要があります。
iOSで使用できるAndroidタイプのトーストコントロールはありません。
あなたはそれのようなものを使用したい場合、あなたはUILabelとのUIViewをカスタマイズしたり、いくつか既に作成トーストタイプのコンポーネントを使用し、以下のようにする必要があります。
コードの1行でカスタマイズ可能なトースト通知をサポートするサードパーティのライブラリがあります。ここでの簡単な例です:
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)
さもないと、
あなた自身で実装する場合。以下のコードを使用してください。
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
})
あなたが表示するようにMBProgressHUDを使用することができますアンドロイドのようなトースト。 MBProgressHUD追加した後、あなたはiOSの中でトーストメッセージを表示するには、この機能を使用することができますこの方法で
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)
をトーストを表示することができます。ただ、ビューの拡張機能を作成し、
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()
})
})
}
}
使用方法のメッセージを、このメソッドを呼び出します。
だけ
Thisビューコントローラからself.view.displayToastMessage("message")
を呼び出すが、私はiOSの中でトーストを示すために使用され、最良のライブラリが同じアプリですアンドロイドとして
またpod
をサポートしており、ポッド名がpod 'Toast'
であり、あなたはそれを
[self.view makeToast:@"YOUR TOAST MESSAGE" duration:TOAST_TIMEOUT position:TOAST_CENTER];
を見せたいところはどこでも実装が行以下、その後
#import <UIView+Toast.h>
あなた
ViewController
で
などとても簡単です
上記のキーの値は
です#define TOAST_TOP @"CSToastPositionTop"
#define TOAST_CENTER @"CSToastPositionCenter"
#define TOAST_BOTTOM @"CSToastPositionBottom"
#define TOAST_TIMEOUT 2.0
iOSのThostViewがありません。私たちのカスタムは、サードパーティのライブラリを使用することができます。 このリンクをたどる -
ここで多くは、次のとおりです。https://www.cocoacontrols.com/search?q =トースト –
あなたがここでアンドロイドで必要なものを、親切に更新してください。 – vaibhav