我面临着将活动指示器相对于屏幕大小居中以及在其superview中居中的问题。这是它当前的设置方式:
self.activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
self.tableBackgroundView.addSubview(self.activityIndicator)
self.activityIndicator.center.x = UIScreen.main.bounds.width / 2水平居中效果很好,但我正在尝试将center.y设置为表格视图的中心。什么是最好的方式来设置它垂直居中的方式,使它可以居中的所有屏幕尺寸?下图显示了没有显式设置center.y的情况

发布于 2020-01-25 07:32:59
将UIActivityIndicatorView或层次结构中的任何视图居中的最好方法是使用NSLayoutConstraints,您可以通过编程使用它们来居中ActivityIndicator,如下所示。
activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
//This is important.
view.addSubview(activityIndicator)
//If the Activity Indicator is not in the correct z-index you can uncomment this line
//view.bringSubview(toFront: activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: tableBackgroundView.centerXAnchor, constant: 0.0),
activityIndicator.centerYAnchor.constraint(equalTo: tableBackgroundView.centerYAnchor, constant: 0.0)
])https://stackoverflow.com/questions/59904901
复制相似问题