-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swift: Positioning Toast on status bar, navigation bar, other #33
Comments
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.
|
@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
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! |
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. |
@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:
In a tableviewcontroller swift file I create a ToastView and show it using the showToast method: 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? |
You have a number of issues with that code, all unrelated to the 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. |
Seems that the only ToastPositions are
.Top
,.Center
, and.Bottom
or a specific 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?
The text was updated successfully, but these errors were encountered: