gpt4 book ai didi

swift - 弹出模式中的中心堆栈 View

转载 作者:行者123 更新时间:2023-11-30 11:53:56 28 4
gpt4 key购买 nike

我试图在弹出 View Controller 中呈现一个加载栏,其下方有一个按钮。我可以单独展示 UIProgressView 。我正在努力将 UIProgressView 和 UIButton 放入堆栈 View 中,并将其呈现在弹出窗口的中心。

我已经在下面发布了我的代码以及我得到的结果 View 。堆栈 View 带有一个按钮,但根据调试器,它位于 x: -25 y: 0 处。我尝试将堆栈 View 中心坐标设置为 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

最佳答案

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

关于swift - 弹出模式中的中心堆栈 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48036178/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com