Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Repaint View Components on Device Rotation

Tags:

swiftui

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 appears. But this value doesn't change when the device orientation changes. I need to redraw all components when the user changes the device orientation.

like image 892
oracode Avatar asked Aug 10 '19 11:08

oracode


People also ask

How do I rotate a view in SwiftUI?

SwiftUI's rotation3DEffect() modifier lets us rotate views in 3D space to create beautiful effects in almost no code. It accepts two parameters: what angle to rotate (in degrees or radians), plus a tuple containing the X, Y, and Z axis around which to perform the rotation.

How to detect orientation change in SwiftUI?

SwiftUI doesn't have a built-in way to detect the user rotating their device between portrait and landscape orientation, but we can make one using a custom modifier by responding to the UIDevice. orientationDidChangeNotification notification.

How do I know what orientation my device is in flutter?

In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.


1 Answers

Here‘s an idiomatic SwiftUI implementation based on a notification publisher:

struct ContentView: View {          @State var orientation = UIDevice.current.orientation      let orientationChanged = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)         .makeConnectable()         .autoconnect()      var body: some View {         Group {             if orientation.isLandscape {                 Text("LANDSCAPE")             } else {                 Text("PORTRAIT")             }         }.onReceive(orientationChanged) { _ in             self.orientation = UIDevice.current.orientation         }     } } 

The output of the publisher (not used above, therefor _ as the block parameter) also contains the key "UIDeviceOrientationRotateAnimatedUserInfoKey" in its userInfo property if you need to know if the rotation should be animated.

like image 200
Koraktor Avatar answered Sep 28 '22 10:09

Koraktor