我正在开发游戏,现在我注意到它在iPhone X上看起来并不像我预期的那样。我需要的是在{X}上放置一个标签,在iPhone X上标签没有显示在所有。所以我读了top right corner
并提出了这个解决方案:
GameViewController:
Safe Area
在我的GameScene中,我在用户
时这样做override func viewDidLoad() {
super.viewDidLoad()
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
self.view.layoutSubviews()
}
override func viewDidLayoutSubviews() {
print("viewDidLayoutSubviews")
print(self.view.safeAreaInsets.top)
print(self.view.safeAreaInsets.bottom)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
scene.scaleMode = .resizeFill
skView.presentScene(scene)
}
这样可行,但我是否真的需要检查override func didMove(to view: SKView) {
var label = SKLabelNode()
if let safeAreaInsets = self.view?.safeAreaInsets {
var y: CGFloat = 0.0
if safeAreaInsets.top == 0.0 { iPhone SE, 6/7/8, Plus
y = self.frame.height - 10
} else { // iPhone X
y = frame.maxY - safeAreaInsets.top - 5
}
label.position = CGPoint(x: self.frame.width -100, y: y)
}
label.text = "Highscore: 0"
label.zPosition = 5
}
是否需要检测我是否需要使用iPhone {1}}?安全区域不应该和手机一样吗?
2:
为了能够获得safeAreaInsets.top == 0.0
我需要调用safeAreaInsets
(可能不是全部,而是其中一些)。如果我不使用这个,safeAreaInsets
总是0.0。这是为什么?
答案 0 :(得分:0)
您可以使用:
if #available(iOS 11.0, *) {
if let window = UIApplication.shared.keyWindow {
let safeAreaBottom = window.safeAreaInsets.bottom
let safeAreaLeft = window.safeAreaInsets.left
let safeAreaRight = window.safeAreaInsets.right
let safeAreaTop = window.safeAreaInsets.top
}
}
答案 1 :(得分:0)
private func safeAreaInsets() -> UIEdgeInsets {
if #available(iOS 11.0, *) {
return UIApplication.shared.windows.first?.safeAreaInsets ?? .zero
} else {
return .zero
}
}