'How do I center a UIActivityIndicatorView programmatically in Swift?
I want to place my activity indicator in the centre of my table view controller programmatically but when I run my code it always appears in the top left hand corner.
Here is my swift code;
let activityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.style = .large
activityIndicator.hidesWhenStopped = true
activityIndicator.color = .white
activityIndicator.startAnimating()
return activityIndicator
}()
tableView.addSubview(activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: tableView.centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: tableView.centerYAnchor)
])
tableView.layoutSubviews()
Solution 1:[1]
I needed to include the line;
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stephen501 |

