Swift Activity Indicator and Label

3.2k views Asked by At

I'm trying to do a simple task of creating a activity indicator with a label that says, "loading" or "saving" or whatever I program it to say when it is running. I can not seem to figure out how to get it to be directly under my activity indicator though, right now it is right along the side of it and I want to to be centered below it.

Here is my code:

public func show(viewController : UIViewController) {
   Async.main {
        self.spinner = UIActivityIndicatorView(frame: CGRectMake(0, 0, self.size, self.size))
        self.activityLabel = UILabel(frame: CGRectMake(0,0,200,200))
       if let spinner = self.spinner {
            spinner.activityIndicatorViewStyle = self.style
            let screenSize: CGRect = UIScreen.mainScreen().bounds
            spinner.center = CGPoint (x: screenSize.width/2 , y: screenSize.height/2)
            spinner.hidesWhenStopped = true
            viewController.view.addSubview(spinner)
            spinner.startAnimating()
            self.activityLabel?.center = CGPoint (x: screenSize.width/2 , y: screenSize.height/1.9  )
            self.activityLabel?.text = self.textmessage
            viewController.view.addSubview(self.activityLabel!)
        }
    }
}

Thanks for any help!

1

There are 1 answers

0
dudeman On

I think the problem you are having with the label being misaligned is the fact that you didn't set the label's textAlignment to .center. You need to add:

self.activityLabel.textAlignment = .center

That should put the text inside the label directly under the spinner.