简体   繁体   English

iOS 13模态演示的控制高度

[英]Control height of iOS 13 modal presentation

I would like to display a view controller (with a date picker and a toolbar in it) with a overall height of 260 pt.我想显示一个总高度为 260 pt 的视图 controller(带有日期选择器和工具栏)。 I have set the preferred explicit size in the storyboard editor, however I believe that only affects the popover size.我已经在 storyboard 编辑器中设置了首选的显式大小,但是我相信这只会影响弹出框的大小。 I have tried all the various combinations of segue/preferred presentation types, and they all display the date picker full screen.我已经尝试了 segue/preferred 演示类型的所有各种组合,它们都全屏显示日期选择器。 Indeed, the functionality works, however the pop up take up the whole screen.确实,该功能有效,但弹出窗口占据了整个屏幕。

界面生成器截图

This is what it looks like:这是它的样子:

模拟器截图

  • create your ChildViewController创建您的 ChildViewController
  • add a Vertical Stack View, put everything you need inside (or any other container view)添加一个垂直堆栈视图,将您需要的所有内容放入其中(或任何其他容器视图)
  • calculate that container's view's height计算容器视图的高度
  • change height of ViewController's View to that将 ViewController 的 View 的高度更改为
  • set corner radius of 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
    }
}

Code Sample here代码示例在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM