Swift的活动指示器仅覆盖部分屏幕

时间:2015-12-04 18:19:10

标签: ios xcode swift uiactivityindicatorview

我尝试使用覆盖整个屏幕的背景来执行活动指示器。我的问题是,当我运行应用程序时,它只覆盖屏幕的一部分。我不确定我在这里做错了什么。

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        self.dismissViewControllerAnimated(true) { () -> Void in

            self.activityIndicator = UIActivityIndicatorView(frame: self.view.frame)
            self.activityIndicator.backgroundColor = UIColor(white: 1.0, alpha: 0.5)
            self.activityIndicator.center = self.view.center
            self.activityIndicator.hidesWhenStopped = true
            self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
            self.view.addSubview(self.activityIndicator)
            self.activityIndicator.startAnimating()
            UIApplication.sharedApplication().beginIgnoringInteractionEvents()

            print("save the data on to core data and onto Parse, then segue to the new input controller")

            let file = PFFile(data: UIImageJPEGRepresentation(image, 1.0)!)
            let insurance = PFObject(className: "Insurance")

            insurance["userId"] = PFUser.currentUser()?.objectId
            insurance["imageFile"] = file as PFFile
            insurance.saveInBackgroundWithBlock({ (success, error ) -> Void in
                self.activityIndicator.stopAnimating()
                UIApplication.sharedApplication().endIgnoringInteractionEvents()
                if (success) {
                    self.performSegueWithIdentifier("segueToDetails", sender: self)
                }else{
                    self.displayAlert("Problems Savings", message: "There was an error with saving the data")
                }
            })
        }
    }

Display with the code above

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

更改此行

self.activityIndicator = UIActivityIndicatorView(frame: self.view.frame)

self.activityIndicator = UIActivityIndicatorView(frame: self.view.bounds)

根据您的屏幕截图,self.view的框架类似于(0, 64, /* some width */, /* some height */)y原点是屏幕下方64点。您复制此框架并为活动指示器指定相同的框架。如果您要将活动指示符添加到y原点位于0点的视图,则可能会这样。由于您将其添加到的视图已经是屏幕上的64点,因此添加具有该帧的活动指示符将使活动指示符在电话屏幕128点下方显示屏幕。我通过添加视图的y来源和活动指标的y来源获得此数字。

答案 1 :(得分:1)

我为解决这个问题所做的是推动最顶层控制器上的加载指示器:

private var topMostController: UIViewController? {
    var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
    while let pVC = presentedVC?.presentedViewController {
        presentedVC = pVC
    }

    return presentedVC
}

你可能想尝试这个,我的一个项目:https://github.com/goktugyil/EZLoadingActivity