14

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

在此处输入图像描述

4

3 回答 3

33

试试这个

if #available(iOS 11.0, *) {
    let window = UIApplication.shared.keyWindow
    let bottomPadding = window?.safeAreaInsets.bottom
}
于 2017-11-20T10:14:09.277 回答
1

您可以尝试将子视图(clear、hidden 或其他)固定到 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")
    }
}

测量距离

于 2017-11-19T02:25:35.283 回答
1

重复以前的答案。将子视图固定到UIViewController. 然后将第二个view.safeAreaLayoutGuide.bottomAnchor固定到视图的锚点。两个子视图都固定父视图的top,leadingtrailing锚点。然后,我假设在 中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)")

}

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

于 2017-11-19T02:35:52.983 回答