如何找出底部安全区域边缘到屏幕底部的距离?

How to find out the distance from the bottom safe area edge to the bottom of the screen?

我需要计算安全区域底部锚点与屏幕底部之间的距离。有没有办法在给定视图的代码中做到这一点?

您可以尝试将子视图(透明、隐藏或其他)固定到 safeAreaLayoutGuide 的底部,并计算此视图底部与 viewDidLayoutSubviews 中视图控制器视图之间的差异。

class ViewController: UIViewController {
    let measuringView = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        measuringView.backgroundColor = .magenta
        measuringView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(measuringView)
        let vConstraint = measuringView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        let heightConstraint = measuringView.heightAnchor.constraint(equalToConstant: 34)
        var constraints = NSLayoutConstraint.constraints(withVisualFormat: "|[measuring]|", options: [], metrics: nil, views: ["measuring": measuringView])
        constraints.append(vConstraint)
        constraints.append(heightConstraint)
        NSLayoutConstraint.activate(constraints)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let measuringBottom = measuringView.frame.origin.y + measuringView.frame.height
        let viewBottom = view.bounds.height
        let distance = abs(measuringBottom - viewBottom)
        print("distance is \(distance) points")
    }
}

重复以前的答案。将子视图固定到 UIViewController 的视图底部。然后将第二个固定到视图的 view.safeAreaLayoutGuide.bottomAnchor 锚点。两个子视图都固定了父视图的 topleadingtrailing 锚点。然后,我假设在 viewDidAppear 中,您可以打印出两个子视图的 frame.maxY 值之间的差异。这应该会给你带来不同。

let viewA = UIView()

let viewB = UIView()

override func viewDidLoad() {

    view.addSubview(viewA)
    view.addSubview(viewB)
    viewA.translateAutoResizingMasksIntoConstraints = false
    viewB.translateAutoResizingMasksIntoConstraints = false


        if #available(iOS 11.0, *) {
            NSLayoutConstraint.activate([viewA.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0),
                                         viewA.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
                                         viewA.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0),
                                         viewA.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0),
                                         viewB.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0),
                                         viewB.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0),
                                         viewB.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0),
                                         viewB.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)])
        } else {
            // Fallback on earlier versions
        }

}

override func viewDidAppear() {

    super.viewDidAppear()
    print("Safe Distance Value is:\(viewA.frame.maxY - viewB.frame.maxY)")

}

作为其他人的参考,该值在 iPhone X 模拟器上似乎是 34

试试这个

if #available(iOS 11.0, *) {
    let window = UIApplication.shared.keyWindow
    let bottomPadding = window?.safeAreaInsets.bottom
}