下面是使用的代码,它在普通屏幕上工作正常,但是当我向下滚动表视图时,加载视图加载但保持在顶部。 每次向下滚动时,如何在UITableView上居中显示加载视图?
extension UIViewController {
func loadWave(_ status: Bool) {
var fadeView: UIView?
if status == true {
fadeView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
fadeView?.backgroundColor = UIColor.clear
fadeView?.alpha = 0
fadeView?.tag = 99
let statusBarHeight: CGFloat = 20
let navBarHeight: CGFloat = 44
let image = UIImage(named: "image")
loadingWaveView = UIImageView(image: image)
loadingWaveView.center = view.center
loadingWaveView.frame = CGRect(x: (UIScreen.main.bounds.width / 2) - (112 / 2), y: (UIScreen.main.bounds.height / 2) - (63 / 2) - statusBarHeight - navBarHeight, width: 112, height: 63)
view.addSubview(fadeView!)
fadeView?.addSubview(loadingWaveView)
fadeView?.fadeTo(alphaValue: 1, withDuration: 0.2)
} else {
for subview in view.subviews {
if subview.tag == 99 {
UIView.animate(withDuration: 0.2, animations: {
subview.alpha = 0
}, completion: { (finished) in
subview.removeFromSuperview()
})
}
}
}
}
}
答案 0 :(得分:1)
您应该设置Autolayout constraints
以使其保持在中间位置。
fadeView?.addSubview(loadingWaveView)
loadingWaveView.translatesAutoresizingMaskIntoConstraints = false
let horizontalConstraint = NSLayoutConstraint(item: loadingWaveView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: loadingWaveView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
view.addConstraint(verticalConstraint)