gpt4 book ai didi

ios - 如何创建一个只有屏幕一半高度的 ViewController?

转载 作者:行者123 更新时间:2023-12-04 10:05:47 26 4
gpt4 key购买 nike

我想要 pageSheet UIModalPresentationStyle segue 的所有功能,但我只希望呈现的 ViewController 显示一半屏幕(参见下图中的示例)。

enter image description here

我使用 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:
enter image description here

最佳答案

我想出了一种方法来做到这一点,我发现这很简单:

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/

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