如何在 Swift 中以编程方式居中 UIActivityIndi​​catorView?

发布于 2025-01-11 05:21:17 字数 802 浏览 1 评论 0原文

我想以编程方式将活动指示器放置在表视图控制器的中心,但是当我运行代码时,它总是出现在左上角。

这是我的快速代码;

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()

activity indicator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远山浅 2025-01-18 05:21:17

您需要设置activityIndi​​cator.translatesAutoresizingMaskIntoConstraints = false

  lazy var activityIndicator: UIActivityIndicatorView = {
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.style = .large
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false
    activityIndicator.hidesWhenStopped = true
    activityIndicator.color = .white
    activityIndicator.startAnimating()
    return activityIndicator
}()

You need to set activityIndicator.translatesAutoresizingMaskIntoConstraints = false

  lazy var activityIndicator: UIActivityIndicatorView = {
    let activityIndicator = UIActivityIndicatorView()
    activityIndicator.style = .large
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false
    activityIndicator.hidesWhenStopped = true
    activityIndicator.color = .white
    activityIndicator.startAnimating()
    return activityIndicator
}()
煮酒 2025-01-18 05:21:17

我需要包括该行;

activityIndicator.translatesAutoresizingMaskIntoConstraints = false

I needed to include the line;

activityIndicator.translatesAutoresizingMaskIntoConstraints = false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文