我想在屏幕中央显示进度指示器,而不是视图.它应该是视图的中心,因为视图是可滚动的.大多数答案仅说明如何将其置于视图中心.我有这个代码: let screenBound = UIScreen.main.bounds let progr
let screenBound = UIScreen.main.bounds
let progressIndc = UIActivityIndicatorView()
progressIndc.frame = CGRect(x: screenBound.width / 2 - 10,
y: screenBound.height / 2 - 10,
width: 20, height: 20)
progressIndc.hidesWhenStopped = true
progressIndc.color = UIColor.gray
progressIndc.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
// self.view is scroll view
self.view.addSubview(progressIndc)
progressIndc.startAnimating()
但它显示在iPhone 7的顶部附近.应该是正确的方法?我还可以使用进度指示器执行阻止弹出对话框.
如果要在滚动时将指示器视图保持在屏幕中心,可以将叠加视图添加到当前最顶层的UIWindow,然后将指示器视图添加到叠加视图:guard let topWindow = UIApplication.shared.windows.last else {return}
let overlayView = UIView(frame: topWindow.bounds)
overlayView.backgroundColor = UIColor.clear
topWindow.addSubview(overlayView)
let hudView = UIActivityIndicatorView()
hudView.bounds = CGRect(x: 0, y: 0, width: 20, height: 20)
overlayView.addSubview(hudView)
hudView.center = overlayView.center
你应该在最顶层的UIViewController视图附加在顶部的UIWindow之后执行此操作,例如,在viewDidAppearmethod中.
