如何在 Swift 中以编程方式居中 UIActivityIndicatorView?
我想以编程方式将活动指示器放置在表视图控制器的中心,但是当我运行代码时,它总是出现在左上角。
这是我的快速代码;
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要设置activityIndicator.translatesAutoresizingMaskIntoConstraints = false
You need to set activityIndicator.translatesAutoresizingMaskIntoConstraints = false
我需要包括该行;
I needed to include the line;