[英]iOS - Display a progress indicator at the center of the screen rather than the view
我想在屏幕中央顯示進度指示器,而不是視圖。 它應該是視圖的中心,因為視圖是可滾動的。 大多數答案僅說明如何將其置於視圖中心。 我有這個代碼:
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的頂部附近。應該是正確的方法? 我還可以使用進度指示器執行阻止彈出對話框。
你可以使用UIView的中心屬性
progressIndc.center = self.view.center
例如
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
progressIndc.center = self.view.center
self.view.addSubview(progressIndc)
progressIndc.startAnimating()
產量
如果要在滾動時將指示器視圖保持在屏幕中心,可以將疊加視圖添加到當前最頂層的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上之后執行此操作,例如,在viewDidAppear
方法中。
你可以嘗試這個在屏幕頂部添加指標。 但這不是很好的解決方案 -
AppDelegate.sharedInstance.window?.addSubview(progressIndc);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.