gpt4 book ai didi

ios - 在 iPhone X 中叠加时 UIView 的顶部位置不佳

转载 作者:行者123 更新时间:2023-11-30 10:54:20 27 4
gpt4 key购买 nike

当在 viewController 顶部按下按钮时,我当前正在覆盖 UIView。我将它在其他屏幕设备中以及在 iPhone X + 设备上运行时定位良好。它的位置不太好。为了使其定位良好,我需要为其当前的 top safeAreaInsets 减去 40 个点。最初的20点是为了应付状态栏。当我这样做时,它可以工作,但在其他没有刘海的 iPhone 上却不起作用。我在这里做错了什么。这是我的代码

func contentFrame(extraTopOffset pOffset: CGFloat = 0) -> CGRect {
var theFrame = self.view.bounds
if let navBar = self.navigationBar {
let topOffset = navBar.frame.origin.y + navBar.frame.size.height
theFrame.size.height -= topOffset
theFrame.origin.y += topOffset
}
if #available(iOS 11.0, *) {
let safeInsets = UIApplication.shared.delegate!.window!?.safeAreaInsets
let topOffset = safeInsets!.top - (20 + pOffset)
theFrame.origin.y += topOffset
theFrame.size.height -= topOffset + safeInsets!.bottom
}
return theFrame
}

我在另一个 viewController 中显示叠加层并覆盖该方法

override func showOverlay() {
self.overlayView.frame = self.contentFrame()
self.view.addSubview(self.overlayView)
}

enter image description here

enter image description here

最佳答案

我通过使用我的实现修复了它。我检查当前设备是否有缺口,如果有,我从中减去 20。然后我调用方法的时候加20分

class var hasNotch: Bool {
if #available(iOS 11.0, *) {
return (self.shared.delegate!.window!!.safeAreaInsets.top > 20)
}
return false
}

func contentFrame(extraTopOffset pOffset: CGFloat = 0) -> CGRect {
var theFrame = self.view.bounds
if let navBar = self.navigationBar {
let topOffset = navBar.frame.origin.y + navBar.frame.size.height
theFrame.size.height -= topOffset
theFrame.origin.y += topOffset
}
if #available(iOS 11.0, *) {
let safeInsets = UIApplication.shared.delegate!.window!?.safeAreaInsets
var extraOffset = safeInsets!.top - 20
if UIApplication.yb_hasNotch {
extraOffset -= pOffset
}
theFrame.origin.y += extraOffset
theFrame.size.height -= extraOffset + safeInsets!.bottom
}
return theFrame
}

关于ios - 在 iPhone X 中叠加时 UIView 的顶部位置不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54126526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com