I would like to keep my layout the same, and only rotate views in-place when the screen orientation changes.
Below is a simple example of what I am trying to acieve. It shows to TestViews horizontally when in portrait mode, and vertically when in landscape mode.
I've made sure that the views themselve does not get re-created in the hope that it would only animate the difference in layout between portrait and landscape, but it makes the views very small, and then animate them in from the corner of the screen.
What I am trying to acieve, is for each view to effectively rotate by 90 degrees when the orientation changes. Is it possible in swiftUI?
[In my example below, I've hacked my AppDelegate to be my model to get rotation notifications from a publisher in it. This needs to be added for the example to work.]
struct TestView: View {
var text: String
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 25.0, style: .continuous)
.foregroundColor(Color(red: 0.2, green: 0.2, blue: 0.2))
Text(verbatim: text)
.font(.system(size: 95))
.foregroundColor(.white)
}.aspectRatio(1.0, contentMode: .fit)
}
}
struct ContentView: View {
@EnvironmentObject var model : AppDelegate
private let testViews: [TestView] = (0...1).map { TestView(text: "\($0)") }
var body: some View {
ZStack {
if !model.isPotrait {
HStack {
VStack {
testViews[0]
testViews[1]
}
}
} else {
VStack {
HStack {
testViews[0]
testViews[1]
}
}
}
}.padding()
}
}