[英]iOS - Display a progress indicator at the center of the screen rather than the view
I want to display a progress indicator at the center of the screen, NOT the view. 我想在屏幕中央显示进度指示器,而不是视图。 It should be the center of the view because the view is scrollable.
它应该是视图的中心,因为视图是可滚动的。 Most answers only tells how to center it in the view.
大多数答案仅说明如何将其置于视图中心。 I have this code:
我有这个代码:
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()
But it shown near the top in iPhone 7. What should be the right way? 但它显示在iPhone 7的顶部附近。应该是正确的方法? I can also do a blocking pop-up dialog with a progress indicator.
我还可以使用进度指示器执行阻止弹出对话框。
you can use center property of UIView 你可以使用UIView的中心属性
progressIndc.center = self.view.center
for eg 例如
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()
output 产量
if you want to keep the indicator view at the center of screen while scrolling, you can add a overlay view to the current topmost UIWindow, then add your indicator view to the overlay view: 如果要在滚动时将指示器视图保持在屏幕中心,可以将叠加视图添加到当前最顶层的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
you should do this after the topmost UIViewController's view was attached on the top UIWindow, for example, in viewDidAppear
method. 你应该在最顶层的UIViewController视图附加在顶部的UIWindow上之后执行此操作,例如,在
viewDidAppear
方法中。
you can try this one to add you indicator on top of the screen. 你可以尝试这个在屏幕顶部添加指标。 but it's not pretty solution -
但这不是很好的解决方案 -
AppDelegate.sharedInstance.window?.addSubview(progressIndc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.