我想显示一个总高度为260 pt的视图控制器(带有日期选择器和工具栏)。我已经在情节提要编辑器中设置了首选的显式大小,但是我相信这只会影响弹出窗口的大小。我已经尝试了segue /首选演示类型的所有各种组合,它们全屏显示日期选择器。确实,该功能有效,但是弹出窗口占据了整个屏幕。
这是它的样子:
答案 0 :(得分:0)
import UIKit
class ChildViewController: UIViewController {
@IBOutlet weak var stackView: UIStackView!
override func updateViewConstraints() {
// distance to top introduced in iOS 13 for modal controllers
// they're now "cards"
let TOP_CARD_DISTANCE: CGFloat = 40.0
// calculate height of everything inside that stackview
var height: CGFloat = 0.0
for v in self.stackView.subviews {
height = height + v.frame.size.height
}
// change size of Viewcontroller's view to that height
self.view.frame.size.height = height
// reposition the view (if not it will be near the top)
self.view.frame.origin.y = UIScreen.main.bounds.height - height - TOP_CARD_DISTANCE
// apply corner radius only to top corners
self.view.roundCorners(corners: [.topLeft, .topRight], radius: 10.0)
super.updateViewConstraints()
}
}
// https://stackoverflow.com/a/41197790/225503
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
代码示例here