如何找出底部安全区域边缘到屏幕底部的距离?
Posted
技术标签:
【中文标题】如何找出底部安全区域边缘到屏幕底部的距离?【英文标题】:How to find out the distance from the bottom safe area edge to the bottom of the screen? 【发布时间】:2017-11-18 23:48:10 【问题描述】:我需要计算安全区底部锚点到屏幕底部的距离。有没有办法在给定视图的代码中做到这一点?
【问题讨论】:
你有什么理由想要那个距离吗?该值可能会根据方向而改变。你为什么不使用view.safeAreaLayoutGuide
和布局锚来为你做这件事?
@user1046037 可能是为了在屏幕底部设置动画然后再次备份
【参考方案1】:
试试这个
if #available(ios 11.0, *)
let window = UIApplication.shared.keyWindow
let bottomPadding = window?.safeAreaInsets.bottom
【讨论】:
也可以从当前视图控制器view
属性self.view.safeAreaInsets
获取这个属性【参考方案2】:
您可以尝试将子视图(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")
【讨论】:
【参考方案3】:重复以前的答案。将子视图固定到 UIViewController
的视图底部。然后将第二个固定到视图的view.safeAreaLayoutGuide.bottomAnchor
锚点。两个子视图都固定父视图的top
、leading
和trailing
锚点。然后,我假设在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
【讨论】:
以上是关于如何找出底部安全区域边缘到屏幕底部的距离?的主要内容,如果未能解决你的问题,请参考以下文章
当光标位于屏幕的顶部或底部边缘时,如何使用 JQuery/Javascript 向下滚动页面?