我想要 pageSheet UIModalPresentationStyle segue 的所有功能,但我只希望呈现的 ViewController 显示一半屏幕(参见下图中的示例)。
我使用 pageSheet modalPresentationStyle 以模态方式呈现它,但它总是以 100% 的高度呈现它。
我无法弄清楚如何限制或修改 ViewController 的高度。我在 SecondViewController 中尝试了以下操作,但没有成功:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = CGSize(width: self.view.frame.width, height: 400)
}
}
我正在使用 Storyboard Segues 和一个以模态方式呈现它的按钮来启动 segue:
最佳答案
我想出了一种方法来做到这一点,我发现这很简单:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let newView = UIView(frame: CGRect(x: 0, y: 500, width: self.view.frame.width, height: 400))
newView.backgroundColor = .yellow
newView.layer.cornerRadius = 20
self.view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
// self.view is now a transparent view, so now I add newView to it and can size it however, I like.
self.view.addSubview(newView)
// works without the tap gesture just fine (only dragging), but I also wanted to be able to tap anywhere and dismiss it, so I added the gesture below
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
self.view.addGestureRecognizer(tap)
}
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
dismiss(animated: true, completion: nil)
}
}
关于ios - 如何创建一个只有屏幕一半高度的 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61598724/