控制iOS 13模式呈现的高度

时间:2019-10-22 15:45:22

标签: ios swift interface-builder segue

我想显示一个总高度为260 pt的视图控制器(带有日期选择器和工具栏)。我已经在情节提要编辑器中设置了首选的显式大小,但是我相信这只会影响弹出窗口的大小。我已经尝试了segue /首选演示类型的所有各种组合,它们全屏显示日期选择器。确实,该功能有效,但是弹出窗口占据了整个屏幕。

interface builder screenshot

这是它的样子:

simulator screenshot

1 个答案:

答案 0 :(得分:0)

  • 创建您的ChildViewController
  • 添加一个垂直堆栈视图,将所需的所有内容放入内部(或任何其他容器视图)
  • 计算该容器视图的高度
  • 将ViewController的View的高度更改为此
  • 设置视图的拐角半径
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