pkluz / PKHUD

A Swift based reimplementation of the Apple HUD (Volume, Ringer, Rotation,…) for iOS 8.
MIT License
3.79k stars 494 forks source link

Make FrameView smaller #209

Open agordeev opened 7 years ago

agordeev commented 7 years ago

What is the proper way to make FrameView twice smaller? It looks too huge to me.

I've read FAQ (https://github.com/pkluz/PKHUD/wiki/FAQ), but there you suggest only to adjust inner subviews, not FrameView itself.

junweimah commented 6 years ago

You found any solution to this?

agordeev commented 6 years ago

Nope :(

aaisataev commented 6 years ago

Now You found any solution to this? :)

daveleenew commented 6 years ago

You found any solution to this?

junweimah commented 6 years ago

Hi everyone.

I found a solution, but not using this library. Instead I have a custom singleton class. here is the code :

import UIKit

open class LoadingIndicator {

    var containerView = UIView()
    var progressView = UIView()
    var activityIndicator = UIActivityIndicatorView()
    var lblLoadingTitle = UILabel()

    open class var shared: LoadingIndicator {
        struct Static {
            static let instance: LoadingIndicator = LoadingIndicator()
        }
        return Static.instance
    }

    open func showProgressView(_ view: UIView, _ loadingTitleString: String) {
        //a view that will cover the whole view controller
        containerView.frame = view.frame
        containerView.center = view.center
        containerView.backgroundColor = UIColor.init(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)

        lblLoadingTitle.text = loadingTitleString
        lblLoadingTitle.sizeToFit()
        lblLoadingTitle.textColor = UIColor.white
        lblLoadingTitle.frame = CGRect(x: (((lblLoadingTitle.layer.bounds.width + 20) / 2) - (lblLoadingTitle.layer.bounds.width / 2)) , y: 60, width: lblLoadingTitle.layer.bounds.width, height: 30) //height was 80

        //this is the black view
        progressView.frame = CGRect(x: 0, y: 0, width: lblLoadingTitle.layer.bounds.width + 20, height: 100) //the +20 is to give a bit of space, if not the whole view will be exactly the width of title label
        progressView.center = view.center
        progressView.backgroundColor = UIColor(hex: 0x444444, alpha: 0.7)
        progressView.clipsToBounds = true
        progressView.layer.cornerRadius = 10

        activityIndicator.frame = CGRect(x: ((progressView.layer.bounds.width / 2) - 20), y: 10, width: 40, height: 40) //the -20 is half of the width of this view itself, if in the future changed to 50, then -25
        activityIndicator.activityIndicatorViewStyle = .whiteLarge

        progressView.addSubview(activityIndicator)
        progressView.addSubview(lblLoadingTitle)
        containerView.addSubview(progressView)
        view.addSubview(containerView)

        activityIndicator.startAnimating()
    }

    open func hideProgressView() {
        activityIndicator.stopAnimating()
        containerView.removeFromSuperview()
    }
}

extension UIColor {

    convenience init(hex: UInt32, alpha: CGFloat) {
        let red = CGFloat((hex & 0xFF0000) >> 16)/256.0
        let green = CGFloat((hex & 0xFF00) >> 8)/256.0
        let blue = CGFloat(hex & 0xFF)/256.0

        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}

Usage :

call this in your view controller

Show:

        LoadingIndicator.shared.showProgressView(self.view, "Loading title you want to show here")

Hide:

        LoadingIndicator.shared.hideProgressView()

@agordeev @aaisataev @daveleenew

agordeev commented 6 years ago

I think using a singleton for activity indicator is a bad idea.

junweimah commented 6 years ago

really? I am a fairly new iOS developer and didn't know that. Can you explain a little?

agordeev commented 6 years ago

Singleton creates unnecessary dependency on some global object (LoadingIndicator in this case). Consider a case when another inactive view controller calls LoadingIndicator.shared.hideProgressView() when the loading indicator is visible and spinning for the active view controller?

junweimah commented 6 years ago

So what do you suggest to be the best practice in cases like this?

Let's say, if I can make sure no other way another vc will call hideProgressView, am I safe?

alexstaravoitau commented 6 years ago

If anyone still needs this, I've added a quick-n-dirty fork: https://github.com/navoshta/PKHUD. It's same as upstream, but uses smaller HUDs. Doesn't work well with labeled HUDs, but if you're only after icon-based ones, it should suffice.