How do I place my activityIndicator programmatically?
Asked Answered
S

4

21

I want to place my activityIndicator where I want it programmatically, but I don´t know how.

I know how to put it in the center of the screen:

activityIndicator.center = self.view.center

But I want something like this:

activityIndicator.activityIndicator(frame: CGRect(x: 100, y: 100, width: 100, height: 50))

But I can´t seem to make it work.

Saffier answered 6/3, 2018 at 12:38 Comment(6)
Where do you add activityIndicator into super view?Bagpipe
Could you elaborate what are you trying to achieve?Fenestration
@AhmadF I want to place my activityIndicator where I want it, more specifically than just putting it in the center of the screen.Saffier
"But I can´t seem to make it work", why? what's the output?Fenestration
Try this url maybe you get the answer #28786215Sausage
Try this url maybe it should help #28786215Sausage
B
27

Basically, you can do this in just a few lines of code:

 func showActivityIndicatory() {
    let activityView = UIActivityIndicatorView(style: .whiteLarge)
    activityView.center = self.view.center
    self.view.addSubview(activityView)
    activityView.startAnimating()
}

If you need more controll on activityView please set Origin of container view to place activityindicator anywhere on the screen.

func showActivityIndicatory() {
    let container: UIView = UIView()
    container.frame = CGRect(x: 0, y: 0, width: 80, height: 80) // Set X and Y whatever you want
    container.backgroundColor = .clear
    
    let activityView = UIActivityIndicatorView(style: .whiteLarge)
    activityView.center = self.view.center
    
    container.addSubview(activityView)
    self.view.addSubview(container)
    activityView.startAnimating()
}
Bagpipe answered 6/3, 2018 at 12:44 Comment(5)
Please check nowBagpipe
is "actInd" typo?Caryloncaryn
to start this showActivityIndicatory(uiView: self.view) how to stop the activity indicator ?Tumbler
@Tumbler you can do activityView.startAnimating() to start the animation and activityView.stopAnimating() to stop the activity indicator animation.Frydman
You don't need activityView.startAnimating() twice. ;) Important: 1. GCRect(...) places the top left corner of the container, better move the center with e.g. container.center = self.view.center. 2. If you move the container to the middle, then move the indicator to activityView.center = self.view.center, it ends up at the bottom right because it moves relatively to the container (because it's a subview). To have both the container and the indicator in the middle, you have to place the container in the middle, then move the indicator to widthOfContainer/2 & heightOfContainer/2.Ruffianism
S
11

You can declare:

var activityView: UIActivityIndicatorView?

And, in your class, create the next methods for showing or hiding the indicator:

func showActivityIndicator() {
    activityView = UIActivityIndicatorView(style: .large)
    activityView?.center = self.view.center
    self.view.addSubview(activityView!)
    activityView?.startAnimating()
}

func hideActivityIndicator(){
    if (activityView != nil){
        activityView?.stopAnimating()
    }
}

This code works for me. Good luck!

Stanzel answered 25/11, 2019 at 11:41 Comment(2)
you don't need this line: if (activityView != nil){ :)Chazan
You should also check if there is a running instance before you create a new instance and either close the running instance first or skip creating the new one.Yore
F
7

Swift 5:

let activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)

    // Place the activity indicator on the center of your current screen
            myActivityIndicator.center = view.center

    // In most cases this will be set to true, so the indicator hides when it stops spinning
            myActivityIndicator.hidesWhenStopped = true

    // Start the activity indicator and place it onto your view
            myActivityIndicator.startAnimating()
            view.addSubview(myActivityIndicator)

    // Do something here, for example fetch the data from API


    // Finally after the job above is done, stop the activity indicator
            myActivityIndicator.stopAnimating()
Frydman answered 21/6, 2019 at 13:5 Comment(2)
// Do something here, for example fetch the data from API - If this is something that's going to take a couple of seconds, you should always do it in a background thread (so you don't block the main one), then switch back to the main thread once you're done and stop the indicator with: DispatchQueue.main.async {self.myActivityIndicator.stopAnimating()}.Ruffianism
@Ruffianism That is correct and I absolutely agree. Good point, it might help someone in the future.Frydman
T
3

Swift 4, Swift 5

Here if you prefer my daily/favorite programmatic pattern

// MARK: - Component
lazy var indicatorView: UIActivityIndicatorView = {
  let view = UIActivityIndicatorView(style: .medium)
  view.color = .white
  view.startAnimating()
  view.translatesAutoresizingMaskIntoConstraints = false
  return view
}()

// MARK: - Life Cycle
override func viewDidLoad() {
  super.viewDidLoad()
  setupViews()
  setupLayouts()
}

func setupViews() {
  addSubview(indicatorView)
}

    
func setupLayouts() {
  NSLayoutConstraint.activate([
    indicatorView.centerXAnchor.constraint(equalTo: centerXAnchor),
    indicatorView.centerYAnchor.constraint(equalTo: centerYAnchor)
  ])
}
Thine answered 2/9, 2021 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.