【问题标题】:Control height of iOS 13 modal presentationiOS 13 模态演示的控制高度
【发布时间】:2019-10-22 15:45:22
【问题描述】:

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

这就是它的样子:

【问题讨论】:

  • 您需要自定义过渡。
  • 在控制器中放置一个 260px 的视图,带有底部约束(没有顶部约束)并呈现它:) 它会正常工作
  • @rmaddy 如果您有关于在哪里可以找到自定义转换指导的点击或链接,这将对我有很大帮助。谢谢!

标签: ios swift interface-builder segue


【解决方案1】:
  • 创建您的 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

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多