आईओएस में एक संदेश प्रदर्शित करना जिसमें एंड्रॉइड में टोस्ट के समान कार्यक्षमता है


121

मुझे यह जानना चाहिए कि क्या आईओएस में कोई तरीका है जो एंड्रॉइड में टोस्ट संदेशों की तरह व्यवहार करता है। यही है, मुझे एक संदेश प्रदर्शित करने की आवश्यकता है जिसे कुछ सेकंड के बाद स्वचालित रूप से खारिज कर दिया जाता है। यह एंड्रॉइड वातावरण में टोस्ट वर्ग की कार्यक्षमता के समान है।


1
आप इस लिंक को जांचना चाहते हैं stackoverflow.com/questions/3522866/android-toast-in-iphone
Emmanuel

लिंक के लिए धन्यवाद इमैनुएल। मैं इसे लागू करने की कोशिश कर रहा हूं
नीलेश कुमार


इस व्यक्ति ने समाधान stackoverflow.com/questions/3522866/…
user3556406

जवाबों:


99

आप MBProgressHUDप्रोजेक्ट का उपयोग कर सकते हैं ।

MBProgressHUDModeTextटोस्ट-जैसे व्यवहार के लिए HUD मोड का उपयोग करें ,

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

// Configure for text only and offset down
hud.mode = MBProgressHUDModeText;
hud.label.text = @"Some message...";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;

[hud hideAnimated:YES afterDelay:3];

यहां छवि विवरण दर्ज करें


1
100% एंड्रॉइड जैसे व्यवहार के लिए मैं सुझाव देता हूं hud.isUserInteractionEnabled = falseताकि आप संदेश दिखाते समय बाकी ऐप के साथ बातचीत कर सकें।
मटिया सी।

@MattiaC। मुझे hud.isUserInteractionEnabled = falseलगता है कि केवल गेट्टर प्रदान किया गया है के लिए मूल्य निर्धारित करने में सक्षम नहीं है।
अनिकेत ठाकुर

यह UI अवरुद्ध है। यह Android के एक के रूप में एक ही व्यवहार नहीं है
रोमन एम

hud.offset = CGPoint(x: 0, y: MBProgressMaxOffset)निचले किनारे पर HUD की स्थिति के लिए उपयोग करें ।
११:१०

86
NSString *message = @"Some message...";

UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                message:message
                                               delegate:nil
                                      cancelButtonTitle:nil
                                      otherButtonTitles:nil, nil];
[toast show];

int duration = 1; // duration in seconds

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [toast dismissWithClickedButtonIndex:0 animated:YES];
});

IOS 9 या बाद के संस्करण के लिए UIAlertViewController का उपयोग करना

NSString *message = @"Some message...";

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
                                                               message:message
                                                        preferredStyle:UIAlertControllerStyleAlert];

[self presentViewController:alert animated:YES completion:nil];

int duration = 1; // duration in seconds

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [alert dismissViewControllerAnimated:YES completion:nil];
});

स्विफ्ट 3.2

let message = "Some message..."
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
self.present(alert, animated: true)

// duration in seconds
let duration: Double = 5

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
    alert.dismiss(animated: true)
}

7
यह बिल्कुल टोस्ट के समान नहीं है - संवाद दिखाते समय अन्य विचारों के साथ उपयोगकर्ता सहभागिता अक्षम है। हो सकता है कि स्क्रीन पर अन्य दृश्यों के साथ बातचीत करने की क्षमता जोड़ने के साथ 'ग्रे आउट आउट' पृष्ठभूमि को हटाने का एक तरीका है (जैसे कि अलर्ट के पीछे मेरे मामले में बटन या पेंटिंग कैनवास)?
हमें वायरल

UIAlertView को iOS9 में चित्रित किया गया है। क्या ऐसा करने का कोई वैकल्पिक तरीका है?
राजीव भाटिया

धन्यवाद, लेकिन क्या मैं टोस्ट प्रदर्शित करने के लिए UIAlertController का उपयोग कर सकता हूं?
राजीव भाटिया

50

एंड्रॉइड में, एक टोस्ट एक छोटा संदेश है जो स्क्रीन पर थोड़े समय के लिए प्रदर्शित होता है और फिर ऐप के साथ उपयोगकर्ता के संपर्क को बाधित किए बिना स्वचालित रूप से गायब हो जाता है।

यहां छवि विवरण दर्ज करें

इसलिए एंड्रॉइड पृष्ठभूमि से आने वाले बहुत से लोग जानना चाहते हैं कि टोस्ट का आईओएस संस्करण क्या है। वर्तमान प्रश्न के अलावा, अन्य समान प्रश्न यहाँ , यहाँ , और यहाँ पाए जा सकते हैं । इसका उत्तर यह है कि iOS में टोस्ट के बराबर कोई सटीक नहीं है । हालांकि, प्रस्तुत किए गए विभिन्न वर्कअराउंड

हालांकि, मेरी सलाह मानक UI विकल्पों के साथ रहना है जो पहले से ही आईओएस के साथ आते हैं। अपने ऐप को देखने और Android संस्करण के समान व्यवहार करने का प्रयास न करें। इस बारे में सोचें कि इसे कैसे रीपैकेज किया जाए ताकि यह एक iOS ऐप की तरह दिखे और महसूस हो। कुछ विकल्पों के लिए निम्न लिंक देखें।

UI को उसी तरह से पुनः डिज़ाइन करने पर विचार करें जो समान जानकारी प्रदान करता है। या, यदि जानकारी बहुत महत्वपूर्ण है, तो एक चेतावनी जवाब हो सकता है।


बहुत अच्छी तरह से, विशेष रूप से यूआई को फिर से काम करने की धारणा। यह निश्चित रूप से डेवलपर्स या डिजाइनरों के लिए सबसे सरल उत्तर नहीं है, लेकिन टोस्ट आसानी से एक और मोबाइल यूएक्स "जंक दराज" बन सकता है, जिसे अक्सर सामग्री के लिए डंपिंग ग्राउंड के रूप में इस्तेमाल किया जाता है कि 1) गंदी और विचलित करने वाली है, या 2) वास्तव में उपयोगी जानकारी है, और , जैसे कि, बैज, आइकन, या इनलाइन स्थिति संदेश के रूप में आवेदन के प्राथमिक सामग्री तत्वों के साथ सीधे संदर्भ में बेहतर प्रस्तुत किया जाएगा।
17

27

स्विफ्ट 4

इस छोटी सी चाल के बारे में कैसे?

func showToast(controller: UIViewController, message : String, seconds: Double) {
    let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
    alert.view.backgroundColor = UIColor.black
    alert.view.alpha = 0.6
    alert.view.layer.cornerRadius = 15

    controller.present(alert, animated: true)

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds) {
        alert.dismiss(animated: true)
    }
}

कॉलिंग का उदाहरण:

showToast(controller: self, message : "This is a test", seconds: 2.0)

आउटपुट:


2
कमाल का दिखता है। धन्यवाद!
जीरो ०

@decades क्या मुद्दा था?
सज्जाद हिसैन खान

1
हाय Sazzad, समस्या जाहिर है, कि यह एक NavigationController की आवश्यकता थी। मैं इसे ऊपर लाने की कोशिश कर रहा था। शायद मेरी टिप्पणी गलत है और अप्रचलित है
दशकों

यह सुनकर खुशी हुई कि यू ने तय किया
सज्जाद हिसैन खान

1
बहुत बहुत धन्यवाद, बहुत ही सटीक समाधान
शक्तिमुथैया

21

स्विफ्ट 3

तीसरे पक्ष के कोड के बिना एक सरल समाधान के लिए:

यहां छवि विवरण दर्ज करें

बस एक सामान्य UIAlertController का उपयोग करें, लेकिन स्टाइल = एक्शनशीट के साथ (नीचे कोड देखें)

let alertDisapperTimeInSeconds = 2.0
let alert = UIAlertController(title: nil, message: "Toast!", preferredStyle: .actionSheet)
self.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + alertDisapperTimeInSeconds) {
  alert.dismiss(animated: true)
}

इस समाधान का लाभ:

  1. Android टोस्ट संदेश की तरह
  2. फिर भी iOS लुक और फील

2
चतुर एक। मैंने इसके बारे में कभी नहीं सोचा होगा, धन्यवाद: D
मेक्सिकम मुसिएन्को


4

यदि आप iOS स्टाइल के साथ एक चाहते हैं, तो इस ढांचे को Github से डाउनलोड करें

iOS टोस्ट अलर्ट व्यू फ्रेमवर्क

एक बार जब आप फ़्रेमवर्क को आयात कर लेते हैं तो यह उदाहरण आप पर UIViewController काम करता है।

उदाहरण 1:

//Manual 
let tav = ToastAlertView()
tav.message = "Hey!"
tav.image = UIImage(named: "img1")!
tav.show()
//tav.dismiss() to Hide

उदाहरण 2:

//Toast Alert View with Time Dissmis Only
self.showToastAlert("5 Seconds",
                image: UIImage(named: "img1")!,
                hideWithTap: false,
                hideWithTime: true,
                hideTime: 5.0)

अंतिम:

टोस्ट अलर्ट देखें छवि उदाहरण


4

स्विफ्ट 4.0:

एक नई स्विफ्ट फ़ाइल बनाएं। (फाइल-नई-फाइल-खाली स्विफ्ट फाइल)। इसे निम्न कोड का नाम UIViewToast.Add रखें।

import UIKit

func /(lhs: CGFloat, rhs: Int) -> CGFloat {
return lhs / CGFloat(rhs)
}

let HRToastDefaultDuration  =   2.0
let HRToastFadeDuration     =   0.2
let HRToastHorizontalMargin : CGFloat  =   10.0
let HRToastVerticalMargin   : CGFloat  =   10.0

let HRToastPositionDefault  =   "bottom"
let HRToastPositionTop      =   "top"
let HRToastPositionCenter   =   "center"

// activity
let HRToastActivityWidth  :  CGFloat  = 100.0
let HRToastActivityHeight :  CGFloat  = 100.0
let HRToastActivityPositionDefault    = "center"

// image size
let HRToastImageViewWidth :  CGFloat  = 80.0
let HRToastImageViewHeight:  CGFloat  = 80.0

// label setting
let HRToastMaxWidth       :  CGFloat  = 0.8;      // 80% of parent view width
let HRToastMaxHeight      :  CGFloat  = 0.8;
let HRToastFontSize       :  CGFloat  = 16.0
let HRToastMaxTitleLines              = 0
let HRToastMaxMessageLines            = 0

// shadow appearance
let HRToastShadowOpacity  : CGFloat   = 0.8
let HRToastShadowRadius   : CGFloat   = 6.0
let HRToastShadowOffset   : CGSize    = CGSize(width: 4.0, height: 4.0)

let HRToastOpacity        : CGFloat   = 0.5
let HRToastCornerRadius   : CGFloat   = 10.0

var HRToastActivityView: UnsafePointer<UIView>?
var HRToastTimer: UnsafePointer<Timer>?
var HRToastView: UnsafePointer<UIView>?


// Color Scheme
let HRAppColor:UIColor = UIColor.black//UIappViewController().appUIColor
let HRAppColor_2:UIColor = UIColor.white


let HRToastHidesOnTap       =   true
let HRToastDisplayShadow    =   false

//HRToast (UIView + Toast using Swift)

extension UIView {

//public methods
func makeToast(message msg: String) {
    self.makeToast(message: msg, duration: HRToastDefaultDuration, position: HRToastPositionDefault as AnyObject)
}

func makeToast(message msg: String, duration: Double, position: AnyObject) {
    let toast = self.viewForMessage(msg: msg, title: nil, image: nil)
    self.showToast(toast: toast!, duration: duration, position: position)
}

func makeToast(message msg: String, duration: Double, position: AnyObject, title: String) {
    let toast = self.viewForMessage(msg: msg, title: title, image: nil)
    self.showToast(toast: toast!, duration: duration, position: position)
}

func makeToast(message msg: String, duration: Double, position: AnyObject, image: UIImage) {
    let toast = self.viewForMessage(msg: msg, title: nil, image: image)
    self.showToast(toast: toast!, duration: duration, position: position)
}

func makeToast(message msg: String, duration: Double, position: AnyObject, title: String, image: UIImage) {
    let toast = self.viewForMessage(msg: msg, title: title, image: image)
    self.showToast(toast: toast!, duration: duration, position: position)
}

func showToast(toast: UIView) {
    self.showToast(toast: toast, duration: HRToastDefaultDuration, position: HRToastPositionDefault as AnyObject)
}

func showToast(toast: UIView, duration: Double, position: AnyObject) {
    let existToast = objc_getAssociatedObject(self, &HRToastView) as! UIView?
    if existToast != nil {
        if let timer: Timer = objc_getAssociatedObject(existToast!, &HRToastTimer) as? Timer {
            timer.invalidate();
        }
        self.hideToast(toast: existToast!, force: false);
    }

    toast.center = self.centerPointForPosition(position: position, toast: toast)
    toast.alpha = 0.0

    if HRToastHidesOnTap {
        let tapRecognizer = UITapGestureRecognizer(target: toast, action: #selector(handleToastTapped(recognizer:)))
        toast.addGestureRecognizer(tapRecognizer)
        toast.isUserInteractionEnabled = true;
        toast.isExclusiveTouch = true;
    }

    self.addSubview(toast)
    objc_setAssociatedObject(self, &HRToastView, toast, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)

    UIView.animate(withDuration: HRToastFadeDuration,
                               delay: 0.0, options: ([.curveEaseOut, .allowUserInteraction]),
                               animations: {
                                toast.alpha = 1.0
    },
                               completion: { (finished: Bool) in
                                let timer = Timer.scheduledTimer(timeInterval: duration, target: self, selector: #selector(self.toastTimerDidFinish(timer:)), userInfo: toast, repeats: false)
                                objc_setAssociatedObject(toast, &HRToastTimer, timer, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    })
}

func makeToastActivity() {
    self.makeToastActivity(position: HRToastActivityPositionDefault as AnyObject)
}

func showToastActivity() {
    self.isUserInteractionEnabled = false
    self.makeToastActivity()
}

func removeToastActivity() {
    self.isUserInteractionEnabled = true
    self.hideToastActivity()

}

func makeToastActivityWithMessage(message msg: String){
    self.makeToastActivity(position: HRToastActivityPositionDefault as AnyObject, message: msg)
}
func makeToastActivityWithMessage(message msg: String,addOverlay: Bool){

    self.makeToastActivity(position: HRToastActivityPositionDefault as AnyObject, message: msg,addOverlay: true)
}

func makeToastActivity(position pos: AnyObject, message msg: String = "",addOverlay overlay: Bool = false) {
    let existingActivityView: UIView? = objc_getAssociatedObject(self, &HRToastActivityView) as? UIView
    if existingActivityView != nil { return }

    let activityView = UIView(frame: CGRect(x:0, y:0, width: self.frame.width, height: self.frame.height))
    activityView.center = self.centerPointForPosition(position: pos, toast: activityView)
    activityView.alpha = 0.0
    activityView.autoresizingMask = ([.flexibleLeftMargin, .flexibleTopMargin, .flexibleRightMargin, .flexibleBottomMargin])
    activityView.layer.cornerRadius = HRToastCornerRadius

    if HRToastDisplayShadow {
        activityView.layer.shadowColor = UIColor.black.cgColor
        activityView.layer.shadowOpacity = Float(HRToastShadowOpacity)
        activityView.layer.shadowRadius = HRToastShadowRadius
        activityView.layer.shadowOffset = HRToastShadowOffset
    }

    let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    activityIndicatorView.center = CGPoint(x:activityView.bounds.size.width / 2, y: activityView.bounds.size.height / 2)
    activityIndicatorView.color = HRAppColor
    activityView.addSubview(activityIndicatorView)
    activityIndicatorView.startAnimating()

    if (!msg.isEmpty){
        activityIndicatorView.frame.origin.y -= 10



        let activityMessageLabel = UILabel(frame: CGRect(x: activityView.bounds.origin.x, y: (activityIndicatorView.frame.origin.y + activityIndicatorView.frame.size.height + 10), width: activityView.bounds.size.width, height: 20))
        activityMessageLabel.textColor = UIColor.white
        activityMessageLabel.font = (msg.count<=10) ? UIFont(name:activityMessageLabel.font.fontName, size: 16) : UIFont(name:activityMessageLabel.font.fontName, size: 16)
        activityMessageLabel.textAlignment = .center
        activityMessageLabel.text = msg + ".."
        if overlay {
            activityMessageLabel.textColor = UIColor.white
            activityView.backgroundColor = HRAppColor.withAlphaComponent(HRToastOpacity)
            activityIndicatorView.color = UIColor.white
        }
        else {
            activityMessageLabel.textColor = HRAppColor
            activityView.backgroundColor = UIColor.clear
            activityIndicatorView.color = HRAppColor
        }

        activityView.addSubview(activityMessageLabel)

    }

    self.addSubview(activityView)

    // associate activity view with self
    objc_setAssociatedObject(self, &HRToastActivityView, activityView, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

    UIView.animate(withDuration: HRToastFadeDuration,
                               delay: 0.0,
                               options: UIViewAnimationOptions.curveEaseOut,
                               animations: {
                                activityView.alpha = 1.0
    },
                               completion: nil)
    self.isUserInteractionEnabled = false
}

func hideToastActivity() {
    self.isUserInteractionEnabled = true
    let existingActivityView = objc_getAssociatedObject(self, &HRToastActivityView) as! UIView?
    if existingActivityView == nil { return }
    UIView.animate(withDuration: HRToastFadeDuration,
                               delay: 0.0,
                               options: UIViewAnimationOptions.curveEaseOut,
                               animations: {
                                existingActivityView!.alpha = 0.0
    },
                               completion: { (finished: Bool) in
                                existingActivityView!.removeFromSuperview()
                                objc_setAssociatedObject(self, &HRToastActivityView, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    })
}

/*
 *  private methods (helper)
 */
func hideToast(toast: UIView) {
    self.isUserInteractionEnabled = true
    self.hideToast(toast: toast, force: false);
}

func hideToast(toast: UIView, force: Bool) {
    let completeClosure = { (finish: Bool) -> () in
        toast.removeFromSuperview()
        objc_setAssociatedObject(self, &HRToastTimer, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    if force {
        completeClosure(true)
    } else {
        UIView.animate(withDuration: HRToastFadeDuration,
                                   delay: 0.0,
                                   options: ([.curveEaseIn, .beginFromCurrentState]),
                                   animations: {
                                    toast.alpha = 0.0
        },
                                   completion:completeClosure)
    }
}

@objc func toastTimerDidFinish(timer: Timer) {
    self.hideToast(toast: timer.userInfo as! UIView)
}

@objc func handleToastTapped(recognizer: UITapGestureRecognizer) {

    // var timer = objc_getAssociatedObject(self, &HRToastTimer) as! NSTimer
    // timer.invalidate()

    self.hideToast(toast: recognizer.view!)
}

func centerPointForPosition(position: AnyObject, toast: UIView) -> CGPoint {
    if position is String {
        let toastSize = toast.bounds.size
        let viewSize  = self.bounds.size
        if position.lowercased == HRToastPositionTop {
            return CGPoint(x: viewSize.width/2, y: toastSize.height/2 + HRToastVerticalMargin)

        } else if position.lowercased == HRToastPositionDefault {
            return CGPoint(x:viewSize.width/2, y:viewSize.height - toastSize.height - 15 - HRToastVerticalMargin)
        } else if position.lowercased == HRToastPositionCenter {
            return CGPoint(x:viewSize.width/2, y:viewSize.height/2)
        }
    } else if position is NSValue {
        return position.cgPointValue
    }

    print("Warning: Invalid position for toast.")
    return self.centerPointForPosition(position: HRToastPositionDefault as AnyObject, toast: toast)
}

func viewForMessage(msg: String?, title: String?, image: UIImage?) -> UIView? {
    if msg == nil && title == nil && image == nil { return nil }

    var msgLabel: UILabel?
    var titleLabel: UILabel?
    var imageView: UIImageView?

    let wrapperView = UIView()
    wrapperView.autoresizingMask = ([.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin])
    wrapperView.layer.cornerRadius = HRToastCornerRadius
    wrapperView.backgroundColor = UIColor.black.withAlphaComponent(HRToastOpacity)

    if HRToastDisplayShadow {
        wrapperView.layer.shadowColor = UIColor.black.cgColor
        wrapperView.layer.shadowOpacity = Float(HRToastShadowOpacity)
        wrapperView.layer.shadowRadius = HRToastShadowRadius
        wrapperView.layer.shadowOffset = HRToastShadowOffset
    }

    if image != nil {
        imageView = UIImageView(image: image)
        imageView!.contentMode = .scaleAspectFit
        imageView!.frame = CGRect(x:HRToastHorizontalMargin, y: HRToastVerticalMargin, width: CGFloat(HRToastImageViewWidth), height: CGFloat(HRToastImageViewHeight))
    }

    var imageWidth: CGFloat, imageHeight: CGFloat, imageLeft: CGFloat
    if imageView != nil {
        imageWidth = imageView!.bounds.size.width
        imageHeight = imageView!.bounds.size.height
        imageLeft = HRToastHorizontalMargin
    } else {
        imageWidth  = 0.0; imageHeight = 0.0; imageLeft   = 0.0
    }

    if title != nil {
        titleLabel = UILabel()
        titleLabel!.numberOfLines = HRToastMaxTitleLines
        titleLabel!.font = UIFont.boldSystemFont(ofSize: HRToastFontSize)
        titleLabel!.textAlignment = .center
        titleLabel!.lineBreakMode = .byWordWrapping
        titleLabel!.textColor = UIColor.white
        titleLabel!.backgroundColor = UIColor.clear
        titleLabel!.alpha = 1.0
        titleLabel!.text = title

        // size the title label according to the length of the text

        let maxSizeTitle = CGSize(width: (self.bounds.size.width * HRToastMaxWidth) - imageWidth, height: self.bounds.size.height * HRToastMaxHeight)

        let expectedHeight = title!.stringHeightWithFontSize(fontSize: HRToastFontSize, width: maxSizeTitle.width)
        titleLabel!.frame = CGRect(x: 0.0, y: 0.0, width: maxSizeTitle.width, height: expectedHeight)
    }

    if msg != nil {
        msgLabel = UILabel();
        msgLabel!.numberOfLines = HRToastMaxMessageLines
        msgLabel!.font = UIFont.systemFont(ofSize: HRToastFontSize)
        msgLabel!.lineBreakMode = .byWordWrapping
        msgLabel!.textAlignment = .center
        msgLabel!.textColor = UIColor.white
        msgLabel!.backgroundColor = UIColor.clear
        msgLabel!.alpha = 1.0
        msgLabel!.text = msg


        let maxSizeMessage = CGSize(width: (self.bounds.size.width * HRToastMaxWidth) - imageWidth, height: self.bounds.size.height * HRToastMaxHeight)
        let expectedHeight = msg!.stringHeightWithFontSize(fontSize: HRToastFontSize, width: maxSizeMessage.width)
        msgLabel!.frame = CGRect(x: 0.0, y: 0.0, width: maxSizeMessage.width, height: expectedHeight)
    }

    var titleWidth: CGFloat, titleHeight: CGFloat, titleTop: CGFloat, titleLeft: CGFloat
    if titleLabel != nil {
        titleWidth = titleLabel!.bounds.size.width
        titleHeight = titleLabel!.bounds.size.height
        titleTop = HRToastVerticalMargin
        titleLeft = imageLeft + imageWidth + HRToastHorizontalMargin
    } else {
        titleWidth = 0.0; titleHeight = 0.0; titleTop = 0.0; titleLeft = 0.0
    }

    var msgWidth: CGFloat, msgHeight: CGFloat, msgTop: CGFloat, msgLeft: CGFloat
    if msgLabel != nil {
        msgWidth = msgLabel!.bounds.size.width
        msgHeight = msgLabel!.bounds.size.height
        msgTop = titleTop + titleHeight + HRToastVerticalMargin
        msgLeft = imageLeft + imageWidth + HRToastHorizontalMargin
    } else {
        msgWidth = 0.0; msgHeight = 0.0; msgTop = 0.0; msgLeft = 0.0
    }

    let largerWidth = max(titleWidth, msgWidth)
    let largerLeft  = max(titleLeft, msgLeft)

    // set wrapper view's frame
    let wrapperWidth  = max(imageWidth + HRToastHorizontalMargin * 2, largerLeft + largerWidth + HRToastHorizontalMargin)
    let wrapperHeight = max(msgTop + msgHeight + HRToastVerticalMargin, imageHeight + HRToastVerticalMargin * 2)
    wrapperView.frame = CGRect(x: 0.0, y: 0.0, width: wrapperWidth, height: wrapperHeight)

    // add subviews
    if titleLabel != nil {
        titleLabel!.frame = CGRect(x: titleLeft, y: titleTop, width: titleWidth, height: titleHeight)
        wrapperView.addSubview(titleLabel!)
    }
    if msgLabel != nil {
        msgLabel!.frame = CGRect(x: msgLeft, y: msgTop, width: msgWidth, height: msgHeight)
        wrapperView.addSubview(msgLabel!)
    }
    if imageView != nil {
        wrapperView.addSubview(imageView!)
    }

    return wrapperView
    }

    }

extension String {

func stringHeightWithFontSize(fontSize: CGFloat,width: CGFloat) -> CGFloat {
    let font = UIFont.systemFont(ofSize: fontSize)
    let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byWordWrapping;
    let attributes = [NSAttributedStringKey.font:font,
                      NSAttributedStringKey.paragraphStyle:paragraphStyle.copy()]

    let text = self as NSString
    let rect = text.boundingRect(with: size, options:.usesLineFragmentOrigin, attributes: attributes, context:nil)
    return rect.size.height
}
}

उपयोग:

  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")

  // Hide Toast
  self.view.hideToast(toast: self.view)
  self.view.hideToast(toast: self.view, force: true)
  self.view.hideToastActivity()

इस कोड में बहुत अधिक त्रुटियां .. जैसे परिवर्तन। केंद्र से श्वेत, श्वेत से श्वेत, चेतन से चेतन। अवधि से चेतन तक (अवधि, बहुत अधिक त्रुटियां .. कृपया इसे ठीक करें ..
सिद्धार्थ

2

Xamarin.IOS का उपयोग करने वाले लोगों के लिए आप इस तरह कर सकते हैं:

new UIAlertView(null, message, null, "OK", null).Show();

यूकीट का उपयोग करना; आवश्यक है।


2

मेरे लिए यह समाधान ठीक काम करता है: https://github.com/cruffenach/CRToast

यहां छवि विवरण दर्ज करें

उदाहरण इसका उपयोग कैसे करें:

    NSDictionary *options = @{
                          kCRToastTextKey : @"Hello World!",
                          kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                          kCRToastBackgroundColorKey : [UIColor redColor],
                          kCRToastAnimationInTypeKey : @(CRToastAnimationTypeGravity),
                          kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeGravity),
                          kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionLeft),
                          kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionRight)
                          };
[CRToastManager showNotificationWithOptions:options
                            completionBlock:^{
                                NSLog(@"Completed");
                            }];

2

3 सेकंड की देरी के लिए स्विफ्ट 4 सिंटैक्स:

present(alertController, animated: true, completion: nil)

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {                 
    self.dismiss(animated: true, completion: nil)     
} 

वाह। मुझे इस तरह की समस्या थी जहाँ आपके कोड ने मुझे यह बताया कि मुझे क्या करना है। धन्यवाद :)
अल वालिद आशिक

1

अलर्ट का उपयोग करके एंड्रॉइड टोस्ट का स्विफ्ट कार्यान्वयन जो 3 सेकंड के बाद फैलता है।

    func showAlertView(title: String?, message: String?) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
    alertController.addAction(okAction)
    self.presentViewController(alertController, animated: true, completion: nil)


    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) {
        print("Bye. Lovvy")
        alertController.dismissViewControllerAnimated(true, completion: nil)
    }
}

बस इसे कॉल करने के लिए:

self.showAlertView("Message sent...", message: nil)

1

स्विफ्ट 2.0 और विचार के लिए https://stackoverflow.com/a/5079536/6144027

                //TOAST
                let alertController = UIAlertController(title: "", message: "This is a Toast.LENGTH_SHORT", preferredStyle: .Alert)
                self!.presentViewController(alertController, animated: true, completion: nil)
                let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC)))
                dispatch_after(delayTime, dispatch_get_main_queue()) {
                    alertController.dismissViewControllerAnimated(true, completion: nil)
                }

1

यहां आपका समाधान है:
नीचे दिए गए कोड को अपने Xcode प्रोजेक्ट में डालें और आनंद लें,

- (void)showMessage:(NSString*)message atPoint:(CGPoint)point {
const CGFloat fontSize = 16;

UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize];
label.text = message;
label.textColor = UIColorFromRGB(0x07575B);
[label sizeToFit];

label.center = point;

[self.view addSubview:label];

[UIView animateWithDuration:0.3 delay:1 options:0 animations:^{
    label.alpha = 0;
} completion:^(BOOL finished) {
    label.hidden = YES;
    [label removeFromSuperview];
}];
}

कैसे इस्तेमाल करे ?

[self showMessage:@"Toast in iOS" atPoint:CGPointMake(160, 695)];

0

फिर से अगर Xamarin पर IOS का उपयोग कर रहा है तो घटक स्टोर में BTProgressHUD नामक एक नया घटक है


0

1) इस लिंक से टोस्ट-नोटिफिकेशन-आईओएस डाउनलोड करें

2) लक्ष्य पर जाएं -> संबंधित फ़ाइलों के लिए "कंपाइलर स्रोत" में चरण और ऐड -ओनो-ओब्जेक्ट-आर्क बनाएँ

3) एक फंक्शन करें और #import "iToast.h"

-(void)showToast :(NSString *)strMessage {
    iToast * objiTost = [iToast makeText:strMessage];
    [objiTost setFontSize:11];
    [objiTost setDuration:iToastDurationNormal];
    [objiTost setGravity:iToastGravityBottom];
    [objiTost show];
}

4) कॉल करें जहां आपको टोस्ट संदेश प्रदर्शित करने की आवश्यकता है

[self showToast:@"This is example text."];

0

मैंने टोस्ट करने का एक सरल तरीका सोचा! बटन के बिना UIAlertController का उपयोग कर! हम अपने संदेश के रूप में बटन पाठ का उपयोग करते हैं! उसे ले लो? नीचे देखें कोड:

func alert(title: String?, message: String?, bdy:String) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let okAction = UIAlertAction(title: bdy, style: .Cancel, handler: nil)
    alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)


    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) {
        //print("Bye. Lovvy")
        alertController.dismissViewControllerAnimated(true, completion: nil) 
    }    
}

इसे इस तरह उपयोग करें:

self.alert(nil,message:nil,bdy:"Simple Toast!") // toast
self.alert(nil,message:nil,bdy:"Alert") // alert with "Alert" button

0

इसी तरह मैंने स्विफ्ट 3.0 में किया है। मैंने UIView एक्सटेंशन बनाया और self.view.showToast (संदेश: "संदेश यहाँ", अवधि: 3.0) और self.view.hideToast () कॉलिंग

extension UIView{
var showToastTag :Int {return 999}

//Generic Show toast
func showToast(message : String, duration:TimeInterval) {

    let toastLabel = UILabel(frame: CGRect(x:0, y:0, width: (self.frame.size.width)-60, height:64))

    toastLabel.backgroundColor = UIColor.gray
    toastLabel.textColor = UIColor.black
    toastLabel.numberOfLines = 0
    toastLabel.layer.borderColor = UIColor.lightGray.cgColor
    toastLabel.layer.borderWidth = 1.0
    toastLabel.textAlignment = .center;
    toastLabel.font = UIFont(name: "HelveticaNeue", size: 17.0)
    toastLabel.text = message
    toastLabel.center = self.center
    toastLabel.isEnabled = true
    toastLabel.alpha = 0.99
    toastLabel.tag = showToastTag
    toastLabel.layer.cornerRadius = 10;
    toastLabel.clipsToBounds  =  true
    self.addSubview(toastLabel)

    UIView.animate(withDuration: duration, delay: 0.1, options: .curveEaseOut, animations: {
        toastLabel.alpha = 0.95
    }, completion: {(isCompleted) in
        toastLabel.removeFromSuperview()
    })
}

//Generic Hide toast
func hideToast(){
    if let view = self.viewWithTag(self.showToastTag){
        view.removeFromSuperview()
    }
  }
}

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.