gpt4 book ai didi

Swift iOS - 如何将 subview 添加到窗口的中心?

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:49 24 4
gpt4 key购买 nike

我有一个在 iPhone 和 iPad 上显示的事件指示器。在 iPad 的分屏模式下,它会呈现给调用它的 View 的任何一侧。相反,我希望它显示在窗口屏幕的中间/中央。如果我这样做,无论是在纵向的 iPhone 上还是在分屏模式下的 iPad 上,它都会始终位于屏幕中央。

我该怎么做?

MyView: UIViewController{

let actInd = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)


@IBAction fileprivate func buttonPressed(_ sender: UIButton) {

guard let window = UIApplication.shared.keyWindow else { return }
//how to add actInd as subview to the window' screen?
actInd.startAnimating()
}

}

最佳答案

这很简单。关闭自动调整大小蒙版。添加add actInd到窗口,然后设置中心anchors .

actInd.translatesAutoresizingMaskIntoConstraints = false

window.addSubview(actInd)
actInd.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
actInd.centerYAnchor.constraint(equalTo: window.centerYAnchor).isActive = true

关于Swift iOS - 如何将 subview 添加到窗口的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521949/

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