[英]Safe area screen size before and after device has rotated (Swift)
當用戶在縱向和橫向之間切換時,我想對 UIView(容器視圖)應用不同的約束(大小、position、顏色等)。
由於某種原因,初始屏幕高度不是安全區域高度 - 旋轉屏幕后似乎沒問題。 我究竟做錯了什么???
“h = 667.0, w = 375.0” 肖像
“h = 343.0, w = 667.0” 橫向
“h = 603.0, w = 375.0” 肖像
“h = 343.0, w = 667.0” 橫向
“h = 603.0, w = 375.0” 肖像
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) { (context) in
self.setVC1Constraints()
}
}
override func viewDidLoad() {
super.viewDidLoad()
addChildVC1()
}
func addChildVC1() {
addChild(childVC1)
view.addSubview(childVC1.view)
setVC1Constraints()
}
func setVC1Constraints() {
let orientation = getScreenOrientation()
let screenHeight = view.safeAreaLayoutGuide.layoutFrame.size.height
let screenWidth = view.safeAreaLayoutGuide.layoutFrame.size.width
print("h = \(screenHeight), w = \(screenWidth)")
childVC1.view.translatesAutoresizingMaskIntoConstraints = false
childVC1.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
childVC1.view.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
if orientation == Orientations.Portrait || orientation == Orientations.PortraitUpsideDown {
print("portrait") }
else if orientation == Orientations.LandscapeRight || orientation == Orientations.LandscapeLeft {
print("landscape") }
}
這是預期的行為,您可以看到差距由導航欄高度和引用 Apple 文檔表示:
安全區域布局指南
布局指南,代表視圖中未被條形和其他內容遮擋的部分。
當視圖在屏幕上可見時,本指南反映了導航欄、選項卡欄、工具欄和其他祖先視圖未覆蓋的視圖部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.