Comments (6)
I figured a neat and easy to implement solution for this without you needing to change the API at all.
What I thought about (and tested and got it working perfectly) is to add the Toast to a different view, and when I want to hide the toast I simply remove the view from its superview and the Toast is removed along with it. Then If another toast needs to be displayed, I simply initialize the same view again and it is a clean view and then there is no toasts left. Some basic sample code:
var viewForToast = UIView()
func showToast(result: String) {
self.viewForToast.removeFromSuperview()
viewForToast = UIView()
viewForToast.frame = self.view.frame
self.viewForToast.makeToast(result, duration: 10.0, position: .center)
self.view.addSubview(viewForToast)
}
from toast-swift.
I'm only a year and a half late on this, but it's now possible in 3.0.0
. You can have ToastManager.shared.isQueueEnabled
set to true
and then you can call self.view.hideToast()
to immediately hide the active toast and present the next one (assuming there's multiple toasts in the queue).
from toast-swift.
I'd prefer to just make hideToast(_:)
public. One of the reasons I never made this public in the first place is because it requires a reference to the toast view, which is a little inconvenient because none of the stock makeToast
methods return a reference to the view they create. If you're presenting your own custom view as toast, this isn't a big deal. However if you were using makeToast
, then it requires you to manually call toastViewForMessage
to get the reference to the view, and then showToast(_:)
.
So, in the interim, I'll release a new version this week that just makes the existing hideToast(_:)
method public. Ultimately though I want to think about how we can elegantly create a hideToast()
method that doesn't require a reference to the toast view. This would be the ideal solution.
from toast-swift.
In addition, I may consider merging this into both Objective-C and Swift versions: scalessec/Toast#81
from toast-swift.
And what do you think of adding something like this in the framework?
In UIView's public extension:
private var currentToast: UIView?
public func hideToast() {
if let currentToast = currentToast {
if let timer = objc_getAssociatedObject(toast, &ToastKeys.Timer) as? NSTimer {
timer.invalidate()
}
self.hideToast(toast, fromTap: true)
}
}
And in makeToast
method:
public func makeToast(message: String?, duration: NSTimeInterval, position: ToastPosition, title: String?, image: UIImage?, style: ToastStyle?, completion: ((didTap: Bool) -> Void)?) {
var toastStyle = ToastManager.shared.style
if let style = style {
toastStyle = style
}
do {
currentToast = try self.toastViewForMessage(message, title: title, image: image, style: toastStyle)
self.showToast(currentToast, duration: duration, position: position, completion: completion)
} catch ToastError.InsufficientData {
print("Error: message, title, and image are all nil")
} catch {}
}
Add currentToast = nextToast
and else clause in the hideToast method:
private func hideToast(toast: UIView, fromTap: Bool) {
....
if let nextToast = self.queue.firstObject as? UIView, duration = objc_getAssociatedObject(nextToast, &ToastKeys.Duration) as? NSNumber, position = objc_getAssociatedObject(nextToast, &ToastKeys.Position) as? NSValue {
currentToast = nextToast
self.queue.removeObjectAtIndex(0)
self.showToast(nextToast, duration: duration.doubleValue, position: position.CGPointValue())
} else {
currentToast = .None
}
}
}
I don't know if I'm missing anything.
from toast-swift.
Is this still open? 🤔
from toast-swift.
Related Issues (20)
- Toast is getting shown behind the view controller HOT 2
- App is crash
- Multiple are not working its showing only one line.
- Toast should move up with keyboard HOT 2
- Can you add a mask?
- Possibility to add a button to close/dismiss the toast message. HOT 1
- Toast make should be inside main dispatch block.
- BitCode
- Toast not getting disappearing
- Toast-Swift not working in Xcode 14.3 HOT 6
- toast一直不消失 HOT 1
- Xcode 15 loader not hiding HOT 1
- Toast-Swift not working in Xcode15, swift 5.9 HOT 2
- libarclite is not in Xcode15
- Privacy Manifests HOT 22
- Xcode 15.2 Compiler Warning: Forming 'UnsafeRawPointer' to an inout variable ... HOT 1
- Privacy Manifest and Signature HOT 1
- Memory Leak
- Any support of Privacy Manifest? HOT 2
- Calling makeToastActivity immediately after calling hideToastActivity does not display ToastActivity.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from toast-swift.