ios - 以编程方式将指示器 View 置于警报 Controller 中

标签 ios swift uiview constraints autoresizingmask

我正在尝试以编程方式将事件指示器置于警报 Controller 的中心,但没有看到预期的结果。

尝试:

import NVActivityIndicatorView

public func displayActivityAlertWithCompletion(ViewController: UIViewController, pending: UIAlertController, completionHandler: @escaping ()->())
{

    //create an activity indicator
    let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))


    //indicator.clipsToBounds = true
    indicator.type = .ballScaleMultiple
    indicator.autoresizingMask = \[.flexibleWidth, .flexibleHeight\]
    indicator.color = UIColor(rgba: Palette.loadingColour)



    //add the activity indicator as a subview of the alert controller's view
    pending.view.addSubview(indicator)
    indicator.isUserInteractionEnabled = false
    // required otherwise if there buttons in the UIAlertController you will not be able to press them
    indicator.startAnimating()



    ViewController.present(pending, animated: true, completion: completionHandler)
}

enter image description here

最佳答案

您面临的问题在于这一行:

let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))

此时您的pending alert controller not 还没有出现,它的框架也没有更新。

在显示警报 Controller 后,您必须更新指标的 frame,例如,您可以执行以下操作。

替换这一行:

ViewController.present(pending, animated: true, completion: completionHandler)

有了这个:

ViewController.present(pending, animated: true, completion: {
    // update frame here:
    indicator.frame = CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50)
    // and manually call completionHandler:
    completionHandler()
})

关于ios - 以编程方式将指示器 View 置于警报 Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51037753/

相关文章:

ios - 如何在听到音乐时使用 AVAudioplayer 快进?

ios - 在 iOS 中访问 Magento Rest API - swift 3.0

uiview - NSPoint、CGPoint、NSSize、CGSize和NSRect、CGRect的单位是什么?

ios - 看不到我的 ios 设备连接到访问 sqlite 数据库

ios - 如何将 UIView 放入 UITableView 并将其固定在底部

ios - 将框架弱链接到静态库

ios - 如何使 UILabel 或 UIButton 具有两行,第一行有一个字符,第二行有两个字符?

uiview - 如何在 Swift 中的主视图中添加 subview

iphone - UIimageView 圆顶角 BezierPath 不起作用?

ios - 如何从应用程序中删除 ios Today 扩展