如何使用swift设计Ipad的框架,按高度覆盖一半屏幕?

How to design a frame for Ipad covering the half of the screen by height using swift?

我想用弹出窗口覆盖 ipad 的一半屏幕,我想为其设计一个框架。 通过对 iphone 使用以下代码可以实现预期的结果,但在 ipad.

的情况下它不起作用
 let frame = CGRect(x: self.moreButton.frame.origin.x + self.stackOfButtons.frame.width / 2 , y: UIScreen.main.bounds.height - self.stackOfButtons.frame.origin.x, width: self.moreButton.frame.width, height: self.moreButton.frame.height)

尝试使用自动布局,声明您的视图:

let dummyView = UIView()

在viewDidLoad中设置dummyView,添加并设置constraints::

dummyView.backgroundColor = .red
dummyView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(dummyView)
dummyView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
dummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
dummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
dummyView.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

红色视图是结果

如果你想像这样从顶部改变约束:

view.addSubview(dummyView)
dummyView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
dummyView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
dummyView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
dummyView.bottomAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

这是结果