我有一个在 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/