SwiftUI Repaint View Components on Device Rotation

后端 未结 12 1899
谎友^
谎友^ 2020-12-01 01:00

How to detect device rotation in SwiftUI and re-draw view components?

I have a @State variable initialized to the value of UIScreen.main.bounds.width when the first

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 01:37

    I tried some of the previous answers, but had a few problems. One of the solutions would work 95% of the time but would screw up the layout every now and again. Other solutions didn't seem to be in tune with SwiftUI's way of doing things. So I came up with my own solution. You might notice that it combines features of several previous suggestions.

    // Device.swift
    import Combine
    import UIKit
    
    final public class Device: ObservableObject {
    
      @Published public var isLandscape: Bool = false
    
    public init() {}
    
    }
    
    //  SceneDelegate.swift
    import SwiftUI
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
        var device = Device()
    
       func scene(_ scene: UIScene, 
            willConnectTo session: UISceneSession, 
            options connectionOptions: UIScene.ConnectionOptions) {
    
            let contentView = ContentView()
                 .environmentObject(device)
            if let windowScene = scene as? UIWindowScene {
            // standard template generated code
            // Yada Yada Yada
    
               let size = windowScene.screen.bounds.size
               device.isLandscape = size.width > size.height
            }
    }
    // more standard template generated code
    // Yada Yada Yada
    func windowScene(_ windowScene: UIWindowScene, 
        didUpdate previousCoordinateSpace: UICoordinateSpace, 
        interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, 
        traitCollection previousTraitCollection: UITraitCollection) {
    
        let size = windowScene.screen.bounds.size
        device.isLandscape = size.width > size.height
    }
    // the rest of the file
    
    // ContentView.swift
    import SwiftUI
    
    struct ContentView: View {
        @EnvironmentObject var device : Device
        var body: some View {
                VStack {
                        if self.device.isLandscape {
                        // Do something
                            } else {
                        // Do something else
                            }
                        }
          }
    } 
    

提交回复
热议问题