Git Product home page Git Product logo

Comments (5)

scalessec avatar scalessec commented on August 18, 2024 2

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.

scalessec avatar scalessec commented on August 18, 2024

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.

dnadri avatar dnadri commented on August 18, 2024

@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!

from toast-swift.

scalessec avatar scalessec commented on August 18, 2024

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.

dnadri avatar dnadri commented on August 18, 2024

@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?

from toast-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.