Skip to content
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

Closed
dnadri opened this issue Aug 1, 2016 · 5 comments
Closed

Swift: Positioning Toast on status bar, navigation bar, other #33

dnadri opened this issue Aug 1, 2016 · 5 comments

Comments

@dnadri
Copy link

dnadri commented Aug 1, 2016

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?

@scalessec
Copy link
Owner

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 notifications@github.com 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.

@dnadri
Copy link
Author

dnadri commented Aug 1, 2016

@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:
toast-swift-screenshot

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!

@scalessec
Copy link
Owner

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.

@dnadri
Copy link
Author

dnadri commented Aug 2, 2016

@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:
toast-swift-screenshot

Do you see what I am doing wrong here? If not, do you have an example showing how to do this?

@scalessec
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants