Flutter中如何区分设备是左横屏还是右横屏?
Got it, let's break this down. You’ve already got the basics of detecting portrait vs landscape, but notch-equipped devices like the iPhone X add a key twist—when you rotate to landscape, the notch shifts to either the left or right edge. That means you need to tailor your safe area usage based on which exact landscape orientation the device is in, and here’s how to do it in Swift:
First, to check the physical device orientation (to know where the notch is placed), you can use the UIDevice class. Here’s the clean, modern Swift syntax (compatible with Swift 3 and later):
// Check if device is in Landscape Right (notch on the right side) if UIDevice.current.orientation == .landscapeRight { // Adjust your layout to account for the right-side notch // Example: Add padding to the right edge or avoid placing UI elements there } // Check if device is in Landscape Left (notch on the left side) else if UIDevice.current.orientation == .landscapeLeft { // Adjust layout for the left-side notch // Example: Shift content away from the left edge to avoid the notch }
If you need to match the interface orientation (instead of physical device orientation, useful if your app supports rotation locking), you can check the window's interface orientation instead:
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene { switch windowScene.interfaceOrientation { case .landscapeRight: // Handle layout for right-notch landscape case .landscapeLeft: // Handle layout for left-notch landscape default: // Handle portrait or other orientations } }
A quick pro tip: You can combine this orientation check with UIView.safeAreaInsets to make even more precise adjustments. The safe area insets will already reflect the notch's position, but if you need custom behavior (like extra padding for specific UI elements), pairing these two will give you full control.
内容的提问来源于stack exchange,提问作者Aaron Halvorsen

