在popover模态的中心堆栈视图

时间:2017-12-30 17:19:39

标签: swift uipopovercontroller uistackview

我试图在弹出式视图控制器中显示一个带有按钮的加载栏。我可以在它上面展示UIProgressView就好了。我正在努力将UIProgressView和UIButton放入堆栈视图并将其显示在popover的中心。

我已经在下面发布了我的代码以及我得到的结果视图。堆栈视图有一个按钮,但它根据调试器位于x:-25 y:0。我尝试将堆栈视图中心坐标设置为self.view.center坐标但不起作用。我有没有正确设置?

// Collection View #1
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let tv = VideoLibrary() // <- Collection View class
    tv.videoSelectionDelegate = self
    tv.modalPresentationStyle = .popover
    tv.popoverPresentationController?.sourceView = self.view
    present(tv, animated: true, completion: nil)
}

// VideoLibrary() / Collection View #2
 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let loadingView = LoadingVideoView() // <- view with stack view
    loadingView.view.frame = self.view.frame
    loadingView.modalPresentationStyle = .overCurrentContext
    present(loadingView, animated: true, completion: nil) 
}


// LoadingVideoView()
var progressView: UIProgressView = {
   let progress = UIProgressView(progressViewStyle: .default)
    progress.progress = 0.5
    return progress
}()
var blurView: UIVisualEffectView = {
   let effect = UIBlurEffect(style: .extraLight)
    let blur = UIVisualEffectView(effect: effect)
    return blur
}()
var stack: UIStackView = {
    let stack = UIStackView(frame: .zero)
    stack.distribution = .equalSpacing
    stack.axis = .vertical
    stack.alignment = .center
    stack.spacing = 20
    return stack
}()
var cancelButton: UIButton = {
    let button = UIButton(type: .roundedRect)
    button.setTitle("Cancel", for: .normal)
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {

    self.view.insertSubview(blurView, at: 0)
    stack.addArrangedSubview(progressView)
    stack.addArrangedSubview(cancelButton)

    stack.translatesAutoresizingMaskIntoConstraints = false
    cancelButton.translatesAutoresizingMaskIntoConstraints = false
    progressView.translatesAutoresizingMaskIntoConstraints = false

    stack.center = self.view.center

    self.view.addSubview(stack)
}

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方法是在stack.center = self.view.center函数调用中设置viewDidAppear(...)。在viewWillAppear(...)函数结束时,中心值相等(根据调试器),但是当调用viewDidAppear(...)时,将更改值。我不知道这两者之间还有什么叫做,但是现在这个有用了。