Comments (5)
You have a number of issues with that code, all unrelated to the Toast
library itself. I just tried this and it works:
class ToastView: UIView {
private let label = UILabel()
var message: String = "" {
didSet {
label.text = message
}
}
override init(frame: CGRect) {
super.init(frame: frame)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
self.backgroundColor = UIColor.redColor()
self.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("not used")
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = self.bounds
}
}
Then in your view controller somewhere:
let toastView = ToastView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: 20.0))
toastView.message = "Hello world"
self.view.window?.windowLevel = UIWindowLevelStatusBar + 1
self.view.window?.showToast(toastView, duration: 2.0, position: CGPoint(x: self.view.frame.size.width / 2.0, y: 10.0), completion: nil)
I just tested this in the demo project and it all works as expected.
from toast-swift.
The toast will display on top of whatever view you present it in. If you want it on top of the navigation bar, present it in the navigation controller's view (eg: use self.navigationController.view instead of self.view like I do in the sample project and then just position it over the navigation bar). To present on top of the status bar present it in the window.
On Aug 1, 2016, at 2:10 AM, David [email protected] wrote:
Are the only ToastPositions .Top, .Center, and .Bottom?
Although a CGPoint can be used, if one would like to position a toast in a particular area, for example in place of the status bar or over the navigation bar, how can one do so?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
from toast-swift.
@scalessec Thank you for your swift response. I've tried presenting the toast on top of/in place of the status bar with the following code in a viewDidLoad()
:
var style = ToastStyle()
style.backgroundColor = UIColor.redColor()
style.messageColor = UIColor.whiteColor()
style.messageFont = UIFont.systemFontOfSize(12.0)
style.cornerRadius = 0.0
self.view.window?.makeToast("Testing toast message on status bar.", duration: 5.0, position: .Top, style: style)
However, here's a screenshot of the resulting toast:
As you can see, the toast is not directly on top of the status bar. Any ideas on how to present the toast in place of the status bar (i.e: Same position, height and width of the status bar) would be greatly appreciated. Thanks!
from toast-swift.
You won't be able to use the out-of-the-box makeToast methods. You'll need to create your own UIView subclass that is exactly 20px high and the width of the device screen and then you'll have to use the showToast methods with your custom view. Use the showToast method that allows for a CGPoint such that you can position it exactly where you want it on the status bar.
from toast-swift.
@scalessec I have been tinkering with your approach for several hours but unfortunately still haven't been able to show the custom UIView toast correctly. Here's what I have done thus far:
import UIKit
class ToastView: UIView {
let currentWindow = UIApplication.sharedApplication().keyWindow
var messageLabel: UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = UIApplication.sharedApplication().statusBarFrame
self.backgroundColor = UIColor.orangeColor()
messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: self.frame.height))
messageLabel.numberOfLines = 0
messageLabel.font = UIFont.systemFontOfSize(12.0)
messageLabel.text = "Testing toast message on status bar. Testing toast message on status bar."
messageLabel.textColor = UIColor.whiteColor()
messageLabel.backgroundColor = UIColor.redColor()
messageLabel.textAlignment = .Center
self.addSubview(messageLabel)
currentWindow?.addSubview(self)
currentWindow?.bringSubviewToFront(self)
messageLabel.center = self.center
//messageLabel.sizeToFit()
}
/*
Only override drawRect: if you perform custom drawing. An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
}
}
*/
In a tableviewcontroller swift file I create a ToastView and show it using the showToast method:
let toast = BTToastView()
self.view.window?.showToast(toast, duration: 5.0, position: CGPoint(x: 0, y: 0), completion: nil)
This strangely results in the toast being displayed in the wrong view controller indefinitely and underneath the status bar with the toast message being cut off instead of expanding to display the entire message:
Do you see what I am doing wrong here? If not, do you have an example showing how to do this?
from toast-swift.
Related Issues (20)
- 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.
- Crashed xcode15.4 HOT 1
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.