简体   繁体   English

iOS - 在屏幕中央而不是视图中显示进度指示器

[英]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.

相关问题 Firebase 图像上传进度屏幕指示器 - Swift iOS - Firebase Image Upload Progress Screen Indicator -Swift iOS 带有UIView指示器的进度视图 - Progress View with UIView indicator iOS卸载视图而不是弹出窗口 - iOS Unload a View Rather Than Pop iOS如何在UITableViewController上向屏幕中心添加视图并将其粘贴到屏幕中心? - iOS how to add a view to screen center and stick it to screen center on a UITableViewController? 将堆栈视图放在IOS屏幕的中央 - Put a stack view at the center of the screen in IOS iOS RestKit上传进度指示器 - iOS RestKit upload progress indicator 进度指示器未显示在正面 - Progress indicator not bring to front view 如何在iOS中以特定部分而非全屏捕获视频 - how to capture a video in specific part rather than full screen in iOS 我可以在容器中而不是全屏显示EPCalendar吗? - Can I display EPCalendar in a container rather than full screen? 单击联系人按钮而不是设备的默认视图时,在自定义表viewcontoller中显示iOS设备中的所有联系人 - Display all contacts from the iOS device in custom table viewcontoller when contacts button is clicked rather than default view of the device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM