2

Using Swift5.5, iOS15.0.1,

To create a PageView is still not that simple in SwiftUI - even in Fall 2021.

Apple gives us the possibility to mis-use a TabView with a special modifier in order to create a PageView

The modifier is called .tabViewStyle(.page(indexDisplayMode: .always)) and it works somewhat until you rotate the phone from Portrait to Landscape.

Apple somehow did not think of Portrait-to-Landscape rotation with this API.

ROTATING THE PHONE MESSES UP THE PAGE-INDEX COMPLETELY !!

Below is the entire code of this example. Try yourself rotating the phone.

See video:

What do you need to to in order to keep the index intact after rotation ?

--> And please, I don't mean to re-gain the index. I am speaking about a nice, smooth rotation around the current index (not some sort of hacky workaround where the index needs to be stored and after rotation re-assigned again...)

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            ScrollView(.init()) {
                TabView {
                    Text("Hello 1")
                    Text("Hello 2")
                    Text("Hello 3")
                    Text("Hello 4")
                }
                .id(UUID())
                .tabViewStyle(.page(indexDisplayMode: .always))
            }            
            .ignoresSafeArea()
            .transition(.move(edge: .bottom))
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
iKK
  • 6,394
  • 10
  • 58
  • 131
  • It is an old bug, I personally surprised apple does not fixed it until now. You can use custom view instead waiting for fix. – ios coder Nov 26 '21 at 17:53
  • Yes, its is really a pity Apple does not fix such an important bug. Do you have a working example of your custom-view idea ? – iKK Nov 26 '21 at 18:11
  • All that is needed, I think, is that TabView's `tabViewStyle(.page(....))` should not re-render the entire TabView once the device is rotated. I don't understand why it does that. I would like it to keep, `index`, `zoom` etc. - simply a nice smooth rotation without this jumping around and crazy behaviour nobody likes. – iKK Nov 26 '21 at 18:16
  • Yes I made a custom TabView from zero without using apple buggy tabView, basically it would be look like as a normal TabView with all features and most important without those bugs. I have not shared the source code some where, because of personal use case. It has so many extended features than the apple api, because I made it and I have full control on it. – ios coder Nov 26 '21 at 18:29

0 Answers0